1 /*
   2  * Copyright (c) 2013, 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 /*
  25  * @test
  26  * @bug 8001326
  27  * @modules java.security.jgss/sun.security.krb5.internal
  28  *          java.security.jgss/sun.security.krb5.internal.rcache
  29  * @run main/othervm ReplayCacheExpunge 16
  30  * @run main/othervm/fail ReplayCacheExpunge 15
  31  * @summary when number of expired entries minus number of good entries
  32  * is more than 30, expunge occurs, and expired entries are forgotten.
  33 */
  34 
  35 import java.util.Random;
  36 import sun.security.krb5.internal.KerberosTime;
  37 import sun.security.krb5.internal.ReplayCache;
  38 import sun.security.krb5.internal.rcache.AuthTimeWithHash;
  39 
  40 public class ReplayCacheExpunge {
  41     static final String client = "dummy@REALM";
  42     static final String server = "server/localhost@REALM";
  43     static final Random rand = new Random();
  44 
  45     public static void main(String[] args) throws Exception {
  46         // Make sure clockskew is default value
  47         System.setProperty("java.security.krb5.conf", "nothing");
  48 
  49         int count = Integer.parseInt(args[0]);
  50         ReplayCache cache = ReplayCache.getInstance("dfl:./");
  51         AuthTimeWithHash a1 =
  52                 new AuthTimeWithHash(client, server, time(-400), 0, hash("1"));
  53         AuthTimeWithHash a2 =
  54                 new AuthTimeWithHash(client, server, time(0), 0, hash("4"));
  55         KerberosTime now = new KerberosTime(time(0)*1000L);
  56         KerberosTime then = new KerberosTime(time(-300)*1000L);
  57 
  58         // Once upon a time, we added a lot of events
  59         for (int i=0; i<count; i++) {
  60             a1 = new AuthTimeWithHash(client, server, time(-400), 0, hash(""));
  61             cache.checkAndStore(then, a1);
  62         }
  63 
  64         // Now, we add a new one. If some conditions hold, the old ones
  65         // will be expunged.
  66         cache.checkAndStore(now, a2);
  67 
  68         // and adding an old one will not trigger any error
  69         cache.checkAndStore(now, a1);
  70     }
  71 
  72     private static String hash(String s) {
  73         char[] data = new char[16];
  74         for (int i=0; i<16; i++) {
  75             if (i < s.length()) data[i] = s.charAt(i);
  76             else data[i] = (char)('0' + rand.nextInt(10));
  77         }
  78         return new String(data);
  79     }
  80 
  81     private static int time(int x) {
  82         return (int)(System.currentTimeMillis()/1000) + x;
  83     }
  84 }