1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *
19 */
20 package org.apache.directory.server.kerberos.shared.replay;
21
22 import java.util.Collection;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.Map;
26
27 import javax.security.auth.kerberos.KerberosPrincipal;
28
29 import org.apache.directory.server.kerberos.shared.messages.value.KerberosTime;
30 import org.apache.directory.server.kerberos.shared.messages.value.types.PrincipalNameType;
31 import org.apache.directory.server.kerberos.shared.replay.InMemoryReplayCache.ReplayCacheEntry;
32 import org.junit.Test;
33
34 import static org.junit.Assert.assertEquals;
35 import static org.junit.Assert.assertNotNull;
36
37 /**
38 * Test the InMemory replay cache
39 *
40 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
41 * @version $Rev: 542147 $, $Date: 2007-05-28 10:14:21 +0200 (Mon, 28 May 2007) $
42 */
43 public class InMemoryReplayCacheTest
44 {
45 /**
46 * Test that the cache is working well. We will create a new entry
47 * every 20 ms, with 10 different serverPrincipals.
48 *
49 * After this period of time, we should only have 25 entries in the cache
50 */
51 @Test
52 public void testCacheSetting() throws Exception
53 {
54 int delay = 500;
55 long clockSkew = 100;
56
57 // Set a delay of 500 ms and a clock skew of 100 ms
58 InMemoryReplayCache cache = new InMemoryReplayCache( clockSkew, delay );
59
60 // Loop for 2 seconds, then check that the cache is clean
61 int i = 0;
62 int nbClient = 20;
63 int nbServer = 10;
64
65 // Inject 100 entries, one every 20 ms
66 while ( i < 100 )
67 {
68 KerberosPrincipal serverPrincipal = new KerberosPrincipal( "server" + i%nbServer + "@APACHE.ORG", PrincipalNameType.KRB_NT_PRINCIPAL.getOrdinal() );
69 KerberosPrincipal clientPrincipal = new KerberosPrincipal( "client" + i%nbClient + "@APACHE.ORG", PrincipalNameType.KRB_NT_PRINCIPAL.getOrdinal() );
70
71 cache.save( serverPrincipal, clientPrincipal, new KerberosTime( System.currentTimeMillis() ), 0 );
72
73 Thread.sleep( 20 );
74 i++;
75 }
76
77 Map<KerberosPrincipal, List<ReplayCacheEntry>> map = cache.getCache();
78
79 // We should have 20 List of entries, as we have injected 20 different
80 // clientPrincipals
81 assertEquals( nbClient, map.size() );
82
83 int nbEntries = 0;
84
85 // Loop into the cache to see how many entries we have
86 Collection<List<ReplayCacheEntry>> entryList = map.values();
87
88 for ( List<ReplayCacheEntry> entries:entryList )
89 {
90 if ( ( entries == null ) || ( entries.size() == 0 ) )
91 {
92 continue;
93 }
94
95 Iterator<ReplayCacheEntry> iterator = entries.iterator();
96
97 while ( iterator.hasNext() )
98 {
99 iterator.next();
100 nbEntries ++;
101 }
102 }
103
104 // We should have some
105 assertNotNull( nbEntries );
106
107 // Wait another delay, so that the cleaning thread will be kicked off
108 Thread.sleep( delay + 50 );
109
110 nbEntries = 0;
111
112 for ( List<ReplayCacheEntry> entries:entryList )
113 {
114 if ( ( entries == null ) || ( entries.size() == 0 ) )
115 {
116 continue;
117 }
118
119 Iterator<ReplayCacheEntry> iterator = entries.iterator();
120
121 while ( iterator.hasNext() )
122 {
123 iterator.next();
124 nbEntries ++;
125 }
126 }
127
128 // We should not have anymore entry in the cache
129 assertEquals( 0, nbEntries );
130 }
131 }