Path: blob/master/test/jdk/javax/security/auth/kerberos/KerberosTixDateTest.java
41152 views
/*1* Copyright (c) 2008, 2014, 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*/2930/*31* Must setup KDC and Kerberos configuration file32*/3334import java.lang.reflect.InvocationTargetException;35import java.lang.reflect.Method;36import java.lang.reflect.Modifier;37import java.util.Date;38import java.io.*;39import javax.security.auth.RefreshFailedException;40import javax.security.auth.kerberos.KerberosPrincipal;41import javax.security.auth.kerberos.KerberosTicket;42import java.util.Base64;4344public class KerberosTixDateTest {4546// Serialized KerberosTicket from JDK6 (encoded in BASE64)47// Note: the KerberosTicket object is created using the same values as48// the KerberosTicket 't' in main(). Deserialization should succeed49// and the deserialized object should equal to 't'.50static String serializedKerberosTix =51"rO0ABXNyACtqYXZheC5zZWN1cml0eS5hdXRoLmtlcmJlcm9zLktlcmJlcm9zVGlja2V0ZqGBbXB3" +52"w7sCAApbAAxhc24xRW5jb2Rpbmd0AAJbQkwACGF1dGhUaW1ldAAQTGphdmEvdXRpbC9EYXRlO0wA" +53"BmNsaWVudHQAMExqYXZheC9zZWN1cml0eS9hdXRoL2tlcmJlcm9zL0tlcmJlcm9zUHJpbmNpcGFs" +54"O1sAD2NsaWVudEFkZHJlc3Nlc3QAF1tMamF2YS9uZXQvSW5ldEFkZHJlc3M7TAAHZW5kVGltZXEA" +55"fgACWwAFZmxhZ3N0AAJbWkwACXJlbmV3VGlsbHEAfgACTAAGc2VydmVycQB+AANMAApzZXNzaW9u" +56"S2V5dAAmTGphdmF4L3NlY3VyaXR5L2F1dGgva2VyYmVyb3MvS2V5SW1wbDtMAAlzdGFydFRpbWVx" +57"AH4AAnhwdXIAAltCrPMX+AYIVOACAAB4cAAAAARhc24xc3IADmphdmEudXRpbC5EYXRlaGqBAUtZ" +58"dBkDAAB4cHcIAAAAAAC8YU54c3IALmphdmF4LnNlY3VyaXR5LmF1dGgua2VyYmVyb3MuS2VyYmVy" +59"b3NQcmluY2lwYWyZp31dDx4zKQMAAHhwdXEAfgAIAAAAEzARoAMCAQGhCjAIGwZjbGllbnR1cQB+" +60"AAgAAAAVGxNKTEFCUy5TRkJBWS5TVU4uQ09NeHBxAH4AC3VyAAJbWlePIDkUuF3iAgAAeHAAAAAg" +61"AAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABxAH4AC3NxAH4ADHVxAH4ACAAAABMwEaAD" +62"AgEBoQowCBsGc2VydmVydXEAfgAIAAAAFRsTSkxBQlMuU0ZCQVkuU1VOLkNPTXhzcgAkamF2YXgu" +63"c2VjdXJpdHkuYXV0aC5rZXJiZXJvcy5LZXlJbXBskoOG6DyvS9cDAAB4cHVxAH4ACAAAABUwE6AD" +64"AgEBoQwECnNlc3Npb25LZXl4cQB+AAs=";6566public static void main(String[] args) throws Exception {67byte[] asn1Bytes = "asn1".getBytes();68KerberosPrincipal client = new KerberosPrincipal("[email protected]");69KerberosPrincipal server = new KerberosPrincipal("[email protected]");70byte[] keyBytes = "sessionKey".getBytes();71long originalTime = 12345678L;72Date inDate = new Date(originalTime);73boolean[] flags = new boolean[9];74flags[8] = true; // renewable75KerberosTicket t = new KerberosTicket(asn1Bytes, client, server,76keyBytes, 1 /*keyType*/, flags, inDate /*authTime*/,77inDate /*startTime*/, inDate /*endTime*/,78inDate /*renewTill*/, null /*clientAddresses*/);79inDate.setTime(0); // for testing the constructor8081testDateImmutability(t, originalTime);82testS11nCompatibility(t); // S11n: Serialization83testDestroy(t);84}8586private static void checkTime(KerberosTicket kt, long timeValue) {87if (kt.getAuthTime().getTime() != timeValue) {88throw new RuntimeException("authTime check fails!");89}90if (kt.getStartTime().getTime() != timeValue) {91throw new RuntimeException("startTime check fails!");92}93if (kt.getEndTime().getTime() != timeValue) {94throw new RuntimeException("endTime check fails!");95}96if (kt.getRenewTill().getTime() != timeValue) {97throw new RuntimeException("renewTill check fails!");98}99}100101private static void testDateImmutability(KerberosTicket t, long origTime)102throws Exception {103// test the constructor104System.out.println("Testing constructor...");105checkTime(t, origTime);106107// test the getAuth/Start/EndTime() & getRenewTill() methods108System.out.println("Testing getAuth/Start/EndTime() & getRenewTill()...");109t.getAuthTime().setTime(0);110t.getStartTime().setTime(0);111t.getEndTime().setTime(0);112t.getRenewTill().setTime(0);113checkTime(t, origTime);114115System.out.println("DateImmutability Test Passed");116}117118private static void checkEqualsAndHashCode(byte[] bytes, KerberosTicket t)119throws IOException, ClassNotFoundException {120ByteArrayInputStream bais = new ByteArrayInputStream(bytes);121KerberosTicket deserializedTicket = (KerberosTicket)122(new ObjectInputStream(bais).readObject());123if (!deserializedTicket.equals(t)) {124throw new RuntimeException("equals() check fails!");125}126if (deserializedTicket.hashCode() != t.hashCode()) {127throw new RuntimeException("hashCode() check fails!");128}129}130131private static void testS11nCompatibility(KerberosTicket t)132throws Exception {133134System.out.println("Testing against KerberosTicket from JDK6...");135byte[] serializedBytes =136Base64.getMimeDecoder().decode(serializedKerberosTix);137checkEqualsAndHashCode(serializedBytes, t);138139System.out.println("Testing against KerberosTicket from current rel...");140ByteArrayOutputStream baos = new ByteArrayOutputStream();141new ObjectOutputStream(baos).writeObject(t);142checkEqualsAndHashCode(baos.toByteArray(), t);143144System.out.println("S11nCompatibility Test Passed");145}146147private static void testDestroy(KerberosTicket t) throws Exception {148t.destroy();149if (!t.isDestroyed()) {150throw new RuntimeException("ticket should have been destroyed");151}152// Although these methods are meaningless, they can be called153for (Method m: KerberosTicket.class.getDeclaredMethods()) {154if (Modifier.isPublic(m.getModifiers())155&& m.getParameterCount() == 0) {156System.out.println("Testing " + m.getName() + "...");157try {158m.invoke(t);159} catch (InvocationTargetException e) {160Throwable cause = e.getCause();161if (cause instanceof RefreshFailedException ||162cause instanceof IllegalStateException) {163// this is OK164} else {165throw e;166}167}168}169}170System.out.println("Destroy Test Passed");171}172}173174175