Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/security/auth/kerberos/KerberosTixDateTest.java
38853 views
/*1* Copyright (c) 2008, 2013, 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 6659990 814777226* @summary test the immutability of the Date fields in KerberosTicket class,27* serialization, and behavior after being destroyed.28* @ignore Must set up KDC and setup Kerberos configuration file29*/3031import java.lang.reflect.InvocationTargetException;32import java.lang.reflect.Method;33import java.lang.reflect.Modifier;34import java.util.Date;35import java.io.*;36import javax.security.auth.RefreshFailedException;37import javax.security.auth.kerberos.KerberosPrincipal;38import javax.security.auth.kerberos.KerberosTicket;39import java.util.Base64;4041public class KerberosTixDateTest {4243// Serialized KerberosTicket from JDK6 (encoded in BASE64)44// Note: the KerberosTicket object is created using the same values as45// the KerberosTicket 't' in main(). Deserialization should succeed46// and the deserialized object should equal to 't'.47static String serializedKerberosTix =48"rO0ABXNyACtqYXZheC5zZWN1cml0eS5hdXRoLmtlcmJlcm9zLktlcmJlcm9zVGlja2V0ZqGBbXB3" +49"w7sCAApbAAxhc24xRW5jb2Rpbmd0AAJbQkwACGF1dGhUaW1ldAAQTGphdmEvdXRpbC9EYXRlO0wA" +50"BmNsaWVudHQAMExqYXZheC9zZWN1cml0eS9hdXRoL2tlcmJlcm9zL0tlcmJlcm9zUHJpbmNpcGFs" +51"O1sAD2NsaWVudEFkZHJlc3Nlc3QAF1tMamF2YS9uZXQvSW5ldEFkZHJlc3M7TAAHZW5kVGltZXEA" +52"fgACWwAFZmxhZ3N0AAJbWkwACXJlbmV3VGlsbHEAfgACTAAGc2VydmVycQB+AANMAApzZXNzaW9u" +53"S2V5dAAmTGphdmF4L3NlY3VyaXR5L2F1dGgva2VyYmVyb3MvS2V5SW1wbDtMAAlzdGFydFRpbWVx" +54"AH4AAnhwdXIAAltCrPMX+AYIVOACAAB4cAAAAARhc24xc3IADmphdmEudXRpbC5EYXRlaGqBAUtZ" +55"dBkDAAB4cHcIAAAAAAC8YU54c3IALmphdmF4LnNlY3VyaXR5LmF1dGgua2VyYmVyb3MuS2VyYmVy" +56"b3NQcmluY2lwYWyZp31dDx4zKQMAAHhwdXEAfgAIAAAAEzARoAMCAQGhCjAIGwZjbGllbnR1cQB+" +57"AAgAAAAVGxNKTEFCUy5TRkJBWS5TVU4uQ09NeHBxAH4AC3VyAAJbWlePIDkUuF3iAgAAeHAAAAAg" +58"AAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABxAH4AC3NxAH4ADHVxAH4ACAAAABMwEaAD" +59"AgEBoQowCBsGc2VydmVydXEAfgAIAAAAFRsTSkxBQlMuU0ZCQVkuU1VOLkNPTXhzcgAkamF2YXgu" +60"c2VjdXJpdHkuYXV0aC5rZXJiZXJvcy5LZXlJbXBskoOG6DyvS9cDAAB4cHVxAH4ACAAAABUwE6AD" +61"AgEBoQwECnNlc3Npb25LZXl4cQB+AAs=";6263public static void main(String[] args) throws Exception {64byte[] asn1Bytes = "asn1".getBytes();65KerberosPrincipal client = new KerberosPrincipal("client");66KerberosPrincipal server = new KerberosPrincipal("server");67byte[] keyBytes = "sessionKey".getBytes();68long originalTime = 12345678L;69Date inDate = new Date(originalTime);70boolean[] flags = new boolean[9];71flags[8] = true; // renewable72KerberosTicket t = new KerberosTicket(asn1Bytes, client, server,73keyBytes, 1 /*keyType*/, flags, inDate /*authTime*/,74inDate /*startTime*/, inDate /*endTime*/,75inDate /*renewTill*/, null /*clientAddresses*/);76inDate.setTime(0); // for testing the constructor7778testDateImmutability(t, originalTime);79testS11nCompatibility(t); // S11n: Serialization80testDestroy(t);81}8283private static void checkTime(KerberosTicket kt, long timeValue) {84if (kt.getAuthTime().getTime() != timeValue) {85throw new RuntimeException("authTime check fails!");86}87if (kt.getStartTime().getTime() != timeValue) {88throw new RuntimeException("startTime check fails!");89}90if (kt.getEndTime().getTime() != timeValue) {91throw new RuntimeException("endTime check fails!");92}93if (kt.getRenewTill().getTime() != timeValue) {94throw new RuntimeException("renewTill check fails!");95}96}9798private static void testDateImmutability(KerberosTicket t, long origTime)99throws Exception {100// test the constructor101System.out.println("Testing constructor...");102checkTime(t, origTime);103104// test the getAuth/Start/EndTime() & getRenewTill() methods105System.out.println("Testing getAuth/Start/EndTime() & getRenewTill()...");106t.getAuthTime().setTime(0);107t.getStartTime().setTime(0);108t.getEndTime().setTime(0);109t.getRenewTill().setTime(0);110checkTime(t, origTime);111112System.out.println("DateImmutability Test Passed");113}114115private static void checkEqualsAndHashCode(byte[] bytes, KerberosTicket t)116throws IOException, ClassNotFoundException {117ByteArrayInputStream bais = new ByteArrayInputStream(bytes);118KerberosTicket deserializedTicket = (KerberosTicket)119(new ObjectInputStream(bais).readObject());120if (!deserializedTicket.equals(t)) {121throw new RuntimeException("equals() check fails!");122}123if (deserializedTicket.hashCode() != t.hashCode()) {124throw new RuntimeException("hashCode() check fails!");125}126}127128private static void testS11nCompatibility(KerberosTicket t)129throws Exception {130131System.out.println("Testing against KerberosTicket from JDK6...");132byte[] serializedBytes =133Base64.getMimeDecoder().decode(serializedKerberosTix);134checkEqualsAndHashCode(serializedBytes, t);135136System.out.println("Testing against KerberosTicket from current rel...");137ByteArrayOutputStream baos = new ByteArrayOutputStream();138new ObjectOutputStream(baos).writeObject(t);139checkEqualsAndHashCode(baos.toByteArray(), t);140141System.out.println("S11nCompatibility Test Passed");142}143144private static void testDestroy(KerberosTicket t) throws Exception {145t.destroy();146if (!t.isDestroyed()) {147throw new RuntimeException("ticket should have been destroyed");148}149// Although these methods are meaningless, they can be called150for (Method m: KerberosTicket.class.getDeclaredMethods()) {151if (Modifier.isPublic(m.getModifiers())152&& m.getParameterCount() == 0) {153System.out.println("Testing " + m.getName() + "...");154try {155m.invoke(t);156} catch (InvocationTargetException e) {157Throwable cause = e.getCause();158if (cause instanceof RefreshFailedException ||159cause instanceof IllegalStateException) {160// this is OK161} else {162throw e;163}164}165}166}167System.out.println("Destroy Test Passed");168}169}170171172