Path: blob/master/test/jdk/sun/security/util/DerInputBuffer/TimeParsing.java
41152 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* @modules java.base/sun.security.util28*/2930import java.io.*;31import java.util.Date;3233import sun.security.util.DerInputStream;3435public class TimeParsing {3637// 10 Aug 2001 17:43:51 GMT38private final static long TIME = 997465431000l;39private final static long TIME_FRACT1 = 997465431700l;40private final static long TIME_FRACT2 = 997465431760l;41private final static long TIME_FRACT3 = 997465431765l;4243// 010810174351Z44private final static byte[] UTC_ZULU =45{0x17, 0x0d, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x37, 0x34, 0x33, 0x35, 0x31, 0x5a};4647// 010810184351+010048private final static byte[] UTC_PLUS1 =49{0x17, 0x11, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x38, 0x34, 0x33, 0x35, 0x31, 0x2b, 0x30, 0x31, 0x30, 0x30};5051// 010810164351-010052private final static byte[] UTC_MINUS1 =53{0x17, 0x11, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x36, 0x34, 0x33, 0x35, 0x31, 0x2d, 0x30, 0x31, 0x30, 0x30};5455// 20010810174351Z56private final static byte[] GEN_ZULU =57{0x18, 0x0f, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x37, 0x34, 0x33, 0x35, 0x31, 0x5a};5859// 20010810184351+010060private final static byte[] GEN_PLUS1 =61{0x18, 0x13, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x38, 0x34, 0x33, 0x35, 0x31, 0x2b, 0x30, 0x31, 0x30, 0x30};6263// 20010810164351-010064private final static byte[] GEN_MINUS1 =65{0x18, 0x13, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x36, 0x34, 0x33, 0x35, 0x31, 0x2d, 0x30, 0x31, 0x30, 0x30};6667// 20010810174351.7Z68private final static byte[] GEN_FRACT1_ZULU =69{0x18, 0x11, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x37, 0x34, 0x33, 0x35, 0x31, 0x2e, 0x37, 0x5a};7071// 20010810174351.76Z72private final static byte[] GEN_FRACT2_ZULU =73{0x18, 0x12, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x37, 0x34, 0x33, 0x35, 0x31, 0x2e, 0x37, 0x36, 0x5a};7475// 20010810174351.765Z76private final static byte[] GEN_FRACT3_ZULU =77{0x18, 0x13, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x37, 0x34, 0x33, 0x35, 0x31, 0x2e, 0x37, 0x36, 0x35, 0x5a};7879// 20010810174351.7654Z80private final static byte[] GEN_FRACT4_ZULU =81{0x18, 0x14, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x37, 0x34, 0x33, 0x35, 0x31, 0x2e, 0x37, 0x36, 0x35, 0x34, 0x5a};8283// 20010810184351.7+010084private final static byte[] GEN_FRACT1_PLUS1 =85{0x18, 0x15, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x38, 0x34, 0x33, 0x35, 0x31, 0x2e, 0x37, 0x2b, 0x30, 0x31, 0x30, 0x30};8687// 20010810184351.76+010088private final static byte[] GEN_FRACT2_PLUS1 =89{0x18, 0x16, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x38, 0x34, 0x33, 0x35, 0x31, 0x2e, 0x37, 0x36, 0x2b, 0x30, 0x31, 0x30, 0x30};9091// 20010810184351.765+010092private final static byte[] GEN_FRACT3_PLUS1 =93{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};9495// 20010810184351.7654+010096private final static byte[] GEN_FRACT4_PLUS1 =97{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};9899// 20010810184351,765+0100100private final static byte[] GEN_FRACT3_COMMA_PLUS1 =101{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};102103// 20010810184351,7654+0100104private final static byte[] GEN_FRACT4_COMMA_PLUS1 =105{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};106107private static Date decodeUTC(byte[] b) throws IOException {108DerInputStream derin = new DerInputStream(b);109return derin.getUTCTime();110}111112private static Date decodeGeneralized(byte[] b) throws IOException {113DerInputStream derin = new DerInputStream(b);114return derin.getGeneralizedTime();115}116117private static void checkUTC(Date d0, byte[] b, String text) throws Exception {118Date d1 = decodeUTC(b);119if( !d0.equals(d1) ) {120throw new Exception("UTCTime " + text + " failed: " + d1.toGMTString());121} else {122System.out.println("UTCTime " + text + " ok");123}124}125126private static void checkGeneralized(Date d0, byte[] b, String text) throws Exception {127Date d1 = decodeGeneralized(b);128if( !d0.equals(d1) ) {129throw new Exception("GeneralizedTime " + text + " failed: " + d1.toGMTString());130} else {131System.out.println("GeneralizedTime " + text + " ok");132}133}134135public static void main(String args[]) throws Exception {136Date d0 = new Date(TIME);137System.out.println(d0.toGMTString());138139checkUTC(d0, UTC_ZULU, "Zulu");140checkUTC(d0, UTC_PLUS1, "+0100");141checkUTC(d0, UTC_MINUS1, "-0100");142143checkGeneralized(d0, GEN_ZULU, "Zulu");144checkGeneralized(d0, GEN_PLUS1, "+0100");145checkGeneralized(d0, GEN_MINUS1, "-0100");146147Date d1 = new Date(TIME_FRACT1);148checkGeneralized(d1, GEN_FRACT1_ZULU, "fractional seconds (Zulu)");149checkGeneralized(d1, GEN_FRACT1_PLUS1, "fractional seconds (+0100)");150151Date d2 = new Date(TIME_FRACT2);152checkGeneralized(d2, GEN_FRACT2_ZULU, "fractional seconds (Zulu)");153checkGeneralized(d2, GEN_FRACT2_PLUS1, "fractional seconds (+0100)");154155Date d3 = new Date(TIME_FRACT3);156checkGeneralized(d3, GEN_FRACT3_ZULU, "fractional seconds (Zulu)");157checkGeneralized(d3, GEN_FRACT3_PLUS1, "fractional seconds (+0100)");158checkGeneralized(d3, GEN_FRACT3_COMMA_PLUS1, "fractional seconds (+0100)");159checkGeneralized(d3, GEN_FRACT4_ZULU, "fractional seconds (Zulu)");160checkGeneralized(d3, GEN_FRACT4_PLUS1, "fractional seconds (+0100)");161checkGeneralized(d3, GEN_FRACT4_COMMA_PLUS1, "fractional seconds (+0100)");162}163}164165166