265N/A/*
2362N/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
265N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
265N/A *
265N/A * This code is free software; you can redistribute it and/or modify it
265N/A * under the terms of the GNU General Public License version 2 only, as
265N/A * published by the Free Software Foundation.
265N/A *
265N/A * This code is distributed in the hope that it will be useful, but WITHOUT
265N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
265N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
265N/A * version 2 for more details (a copy is included in the LICENSE file that
265N/A * accompanied this code).
265N/A *
265N/A * You should have received a copy of the GNU General Public License version
265N/A * 2 along with this work; if not, write to the Free Software Foundation,
265N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
265N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
265N/A */
265N/A
265N/A/*
265N/A * @test
265N/A * @bug 6659990
265N/A * @summary test the immutability of the Date fields in KerberosTicket class.
267N/A * @ignore Must set up KDC and setup Kerberos configuration file
265N/A */
265N/A
265N/Aimport java.net.InetAddress;
265N/Aimport java.util.Date;
265N/Aimport java.io.*;
265N/Aimport javax.security.auth.kerberos.KerberosKey;
265N/Aimport javax.security.auth.kerberos.KerberosPrincipal;
265N/Aimport javax.security.auth.kerberos.KerberosTicket;
265N/Aimport sun.misc.BASE64Decoder;
265N/A
265N/Apublic class KerberosTixDateTest {
265N/A
265N/A // Serialized KerberosTicket from JDK6 (encoded in BASE64)
265N/A // Note: the KerberosTicket object is created using the same values as
265N/A // the KerberosTicket 't' in main(). Deserialization should succeed
265N/A // and the deserialized object should equal to 't'.
265N/A static String serializedKerberosTix =
265N/A"rO0ABXNyACtqYXZheC5zZWN1cml0eS5hdXRoLmtlcmJlcm9zLktlcmJlcm9zVGlja2V0ZqGBbXB3" +
265N/A"w7sCAApbAAxhc24xRW5jb2Rpbmd0AAJbQkwACGF1dGhUaW1ldAAQTGphdmEvdXRpbC9EYXRlO0wA" +
265N/A"BmNsaWVudHQAMExqYXZheC9zZWN1cml0eS9hdXRoL2tlcmJlcm9zL0tlcmJlcm9zUHJpbmNpcGFs" +
265N/A"O1sAD2NsaWVudEFkZHJlc3Nlc3QAF1tMamF2YS9uZXQvSW5ldEFkZHJlc3M7TAAHZW5kVGltZXEA" +
265N/A"fgACWwAFZmxhZ3N0AAJbWkwACXJlbmV3VGlsbHEAfgACTAAGc2VydmVycQB+AANMAApzZXNzaW9u" +
265N/A"S2V5dAAmTGphdmF4L3NlY3VyaXR5L2F1dGgva2VyYmVyb3MvS2V5SW1wbDtMAAlzdGFydFRpbWVx" +
265N/A"AH4AAnhwdXIAAltCrPMX+AYIVOACAAB4cAAAAARhc24xc3IADmphdmEudXRpbC5EYXRlaGqBAUtZ" +
265N/A"dBkDAAB4cHcIAAAAAAC8YU54c3IALmphdmF4LnNlY3VyaXR5LmF1dGgua2VyYmVyb3MuS2VyYmVy" +
265N/A"b3NQcmluY2lwYWyZp31dDx4zKQMAAHhwdXEAfgAIAAAAEzARoAMCAQGhCjAIGwZjbGllbnR1cQB+" +
265N/A"AAgAAAAVGxNKTEFCUy5TRkJBWS5TVU4uQ09NeHBxAH4AC3VyAAJbWlePIDkUuF3iAgAAeHAAAAAg" +
265N/A"AAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABxAH4AC3NxAH4ADHVxAH4ACAAAABMwEaAD" +
265N/A"AgEBoQowCBsGc2VydmVydXEAfgAIAAAAFRsTSkxBQlMuU0ZCQVkuU1VOLkNPTXhzcgAkamF2YXgu" +
265N/A"c2VjdXJpdHkuYXV0aC5rZXJiZXJvcy5LZXlJbXBskoOG6DyvS9cDAAB4cHVxAH4ACAAAABUwE6AD" +
265N/A "AgEBoQwECnNlc3Npb25LZXl4cQB+AAs=";
265N/A
265N/A public static void main(String[] args) throws Exception {
265N/A byte[] asn1Bytes = "asn1".getBytes();
265N/A KerberosPrincipal client = new KerberosPrincipal("client");
265N/A KerberosPrincipal server = new KerberosPrincipal("server");
265N/A byte[] keyBytes = "sessionKey".getBytes();
265N/A long originalTime = 12345678L;
265N/A Date inDate = new Date(originalTime);
265N/A boolean[] flags = new boolean[9];
265N/A flags[8] = true; // renewable
265N/A KerberosTicket t = new KerberosTicket(asn1Bytes, client, server,
265N/A keyBytes, 1 /*keyType*/, flags, inDate /*authTime*/,
265N/A inDate /*startTime*/, inDate /*endTime*/,
265N/A inDate /*renewTill*/, null /*clientAddresses*/);
265N/A inDate.setTime(0); // for testing the constructor
265N/A
265N/A testDateImmutability(t, originalTime);
265N/A testS11nCompatibility(t); // S11n: Serialization
265N/A }
265N/A
265N/A private static void checkTime(KerberosTicket kt, long timeValue) {
265N/A if (kt.getAuthTime().getTime() != timeValue) {
265N/A throw new RuntimeException("authTime check fails!");
265N/A }
265N/A if (kt.getStartTime().getTime() != timeValue) {
265N/A throw new RuntimeException("startTime check fails!");
265N/A }
265N/A if (kt.getEndTime().getTime() != timeValue) {
265N/A throw new RuntimeException("endTime check fails!");
265N/A }
265N/A if (kt.getRenewTill().getTime() != timeValue) {
265N/A throw new RuntimeException("renewTill check fails!");
265N/A }
265N/A }
265N/A
265N/A private static void testDateImmutability(KerberosTicket t, long origTime)
265N/A throws Exception {
265N/A // test the constructor
265N/A System.out.println("Testing constructor...");
265N/A checkTime(t, origTime);
265N/A
265N/A // test the getAuth/Start/EndTime() & getRenewTill() methods
265N/A System.out.println("Testing getAuth/Start/EndTime() & getRenewTill()...");
265N/A t.getAuthTime().setTime(0);
265N/A t.getStartTime().setTime(0);
265N/A t.getEndTime().setTime(0);
265N/A t.getRenewTill().setTime(0);
265N/A checkTime(t, origTime);
265N/A
265N/A System.out.println("DateImmutability Test Passed");
265N/A }
265N/A
265N/A private static void checkEqualsAndHashCode(byte[] bytes, KerberosTicket t)
265N/A throws IOException, ClassNotFoundException {
265N/A ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
265N/A KerberosTicket deserializedTicket = (KerberosTicket)
265N/A (new ObjectInputStream(bais).readObject());
265N/A if (!deserializedTicket.equals(t)) {
265N/A throw new RuntimeException("equals() check fails!");
265N/A }
265N/A if (deserializedTicket.hashCode() != t.hashCode()) {
265N/A throw new RuntimeException("hashCode() check fails!");
265N/A }
265N/A }
265N/A
265N/A private static void testS11nCompatibility(KerberosTicket t)
265N/A throws Exception {
265N/A
265N/A System.out.println("Testing against KerberosTicket from JDK6...");
265N/A byte[] serializedBytes =
265N/A new BASE64Decoder().decodeBuffer(serializedKerberosTix);
265N/A checkEqualsAndHashCode(serializedBytes, t);
265N/A
265N/A System.out.println("Testing against KerberosTicket from current rel...");
265N/A ByteArrayOutputStream baos = new ByteArrayOutputStream();
265N/A new ObjectOutputStream(baos).writeObject(t);
265N/A checkEqualsAndHashCode(baos.toByteArray(), t);
265N/A
265N/A System.out.println("S11nCompatibility Test Passed");
265N/A }
265N/A}