1 /*
   2  * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 /*
  24  * @test
  25  * @bug 6590930
  26  * @run main/othervm TimeInCCache
  27  * @summary read/write does not match for ccache
  28  */
  29 
  30 import java.io.ByteArrayInputStream;
  31 import java.lang.reflect.Field;
  32 import java.lang.reflect.Method;
  33 import java.nio.file.Files;
  34 import java.nio.file.Paths;
  35 
  36 import sun.security.krb5.internal.ccache.CCacheInputStream;
  37 import sun.security.krb5.internal.ccache.Credentials;
  38 
  39 public class TimeInCCache {
  40     // Attention: this field is also used by CorruptedCC.java test
  41     public static byte[] ccache;
  42 
  43     static {
  44         // A trivial cache file, with startdate and renewTill being zero.
  45         // The endtime is set to sometime in year 2022, so that isValid()
  46         // will always check starttime.
  47         String var =
  48             /*0000*/ "05 04 00 0C 00 01 00 08 FF FF FF 13 FF FE 59 33 " +
  49             /*0010*/ "00 00 00 01 00 00 00 01 00 00 00 0A 4D 41 58 49 " +
  50             /*0020*/ "2E 4C 4F 43 41 4C 00 00 00 05 64 75 6D 6D 79 00 " +
  51             /*0030*/ "00 00 01 00 00 00 01 00 00 00 0A 4D 41 58 49 2E " +
  52             /*0040*/ "4C 4F 43 41 4C 00 00 00 05 64 75 6D 6D 79 00 00 " +
  53             /*0050*/ "00 00 00 00 00 02 00 00 00 0A 4D 41 58 49 2E 4C " +
  54             /*0060*/ "4F 43 41 4C 00 00 00 06 6B 72 62 74 67 74 00 00 " +
  55             /*0070*/ "00 0A 4D 41 58 49 2E 4C 4F 43 41 4C 00 11 00 00 " +
  56             /*0080*/ "00 10 B2 AB A6 CE BC 73 44 08 D9 93 5B 3D EF E5 " +
  57             /*0090*/ "86 88 47 45 10 87 00 00 00 00 62 45 10 87 00 00 " +
  58             /*00A0*/ "00 00 00 40 E0 00 00 00 00 00 00 00 00 00 00 00 " +
  59             /*00B0*/ "00 01 00 61 81 FD 30 81 FA A0 03 02 01 05 A1 0C " +
  60             /*00C0*/ "1B 0A 4D 41 58 49 2E 4C 4F 43 41 4C A2 1F 30 1D " +
  61             /*00D0*/ "A0 03 02 01 00 A1 16 30 14 1B 06 6B 72 62 74 67 " +
  62             /*00E0*/ "74 1B 0A 4D 41 58 49 2E 4C 4F 43 41 4C A3 81 C3 " +
  63             /*00F0*/ "30 81 C0 A0 03 02 01 11 A1 03 02 01 01 A2 81 B3 " +
  64             /*0100*/ "04 81 B0 2B 41 BE 22 15 DE 25 23 20 32 F2 7A 4D " +
  65             /*0110*/ "FD E3 25 63 32 7D D5 A0 B2 55 17 29 B0 44 02 93 " +
  66             /*0120*/ "E5 26 D7 B8 E0 7F 3F B4 EA 51 21 8E E2 68 7D AF " +
  67             /*0130*/ "E3 46 E7 17 64 B5 E7 3E 88 B2 C3 9C B6 32 8B 81 " +
  68             /*0140*/ "F0 4F 96 3E D9 5B 64 F6 17 A8 EE D1 33 ED 71 12 " +
  69             /*0150*/ "62 9B 1F 62 16 AF 0B D7 D6 43 57 5C FE 2A CA 4F " +
  70             /*0160*/ "31 A6 2B DB 5A 9A 7D 3E A8 B3 64 66 17 C7 CD 26 " +
  71             /*0170*/ "44 D4 C7 9A 67 FA 55 C6 4A 8B A9 43 99 DC 6E 86 " +
  72             /*0180*/ "73 0C 76 96 8E CD 4F 44 20 A5 CB FB CD 59 48 46 " +
  73             /*0190*/ "7B F4 A1 09 28 E2 8B 4A 4D 26 5B 7E AE 11 62 62 " +
  74             /*01A0*/ "CF 4E 24 24 67 B4 9C E9 76 A4 F8 50 67 E9 9E 38 " +
  75             /*01B0*/ "15 41 B3 00 00 00 00 ";
  76         ccache = new byte[var.length()/3];
  77         for (int i=0; i<ccache.length; i++) {
  78             ccache[i] = Integer.valueOf(var.substring(3*i,3*i+2), 16).byteValue();
  79         }
  80     }
  81 
  82     public static void main(String[] args) throws Exception {
  83         System.setProperty("sun.security.krb5.debug", "true");  // test code changes in DEBUG
  84         CCacheInputStream cis = new CCacheInputStream(new ByteArrayInputStream(ccache));
  85         cis.readVersion();
  86         cis.readTag();
  87         cis.readPrincipal(0x504);
  88         Method m = CCacheInputStream.class.getDeclaredMethod("readCred", Integer.TYPE);
  89         m.setAccessible(true);
  90         Credentials c = (Credentials) m.invoke(cis, new Integer(0x504));
  91         sun.security.krb5.Credentials cc = c.setKrbCreds();
  92 
  93         // 1. Make sure starttime is still null
  94         if (cc.getStartTime() != null) {
  95             throw new Exception("Fail, starttime should be zero here");
  96         }
  97 
  98         // 2. Make sure renewTill is still null
  99         if (cc.getRenewTill() != null) {
 100             throw new Exception("Fail, renewTill should be zero here");
 101         }
 102 
 103         // 3. Make sure isValid works
 104         c.isValid();
 105     }
 106 }