cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cohei...@apache.org
Subject svn commit: r1309333 - in /cxf/trunk: rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/ rt/ws/security/src/test/java/org/apache/cxf/ws/security/tokenstore/ services/sts/sts-core/src/main/java/org/apache/cxf/sts/cache/ services/sts/sts...
Date Wed, 04 Apr 2012 11:25:58 GMT
Author: coheigea
Date: Wed Apr  4 11:25:57 2012
New Revision: 1309333

URL: http://svn.apache.org/viewvc?rev=1309333&view=rev
Log:
[CXF-4129] - Added some unit tests for security token caching

Added:
    cxf/trunk/rt/ws/security/src/test/java/org/apache/cxf/ws/security/tokenstore/
    cxf/trunk/rt/ws/security/src/test/java/org/apache/cxf/ws/security/tokenstore/EHCacheTokenStoreTest.java
    cxf/trunk/rt/ws/security/src/test/java/org/apache/cxf/ws/security/tokenstore/MemoryTokenStoreTest.java
      - copied, changed from r1309068, cxf/trunk/services/sts/sts-core/src/test/java/org/apache/cxf/sts/cache/DefaultInMemoryTokenStoreTest.java
Removed:
    cxf/trunk/services/sts/sts-core/src/test/java/org/apache/cxf/sts/cache/DefaultInMemoryTokenStoreTest.java
Modified:
    cxf/trunk/rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/EHCacheTokenStore.java
    cxf/trunk/rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/MemoryTokenStore.java
    cxf/trunk/services/sts/sts-core/src/main/java/org/apache/cxf/sts/cache/HazelCastTokenStore.java
    cxf/trunk/services/sts/sts-core/src/test/java/org/apache/cxf/sts/cache/HazelCastTokenStoreTest.java

Modified: cxf/trunk/rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/EHCacheTokenStore.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/EHCacheTokenStore.java?rev=1309333&r1=1309332&r2=1309333&view=diff
==============================================================================
--- cxf/trunk/rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/EHCacheTokenStore.java
(original)
+++ cxf/trunk/rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/EHCacheTokenStore.java
Wed Apr  4 11:25:57 2012
@@ -83,14 +83,18 @@ public class EHCacheTokenStore implement
     public void add(SecurityToken token) {
         if (token != null && !StringUtils.isEmpty(token.getId())) {
             int parsedTTL = getTTL(token);
-            cache.put(new Element(token.getId(), token, false, parsedTTL, parsedTTL));
+            if (parsedTTL > 0) {
+                cache.put(new Element(token.getId(), token, false, parsedTTL, parsedTTL));
+            }
         }
     }
     
     public void add(String identifier, SecurityToken token) {
         if (token != null && !StringUtils.isEmpty(identifier)) {
             int parsedTTL = getTTL(token);
-            cache.put(new Element(identifier, token, false, parsedTTL, parsedTTL));
+            if (parsedTTL > 0) {
+                cache.put(new Element(identifier, token, false, parsedTTL, parsedTTL));
+            }
         }
     }
     
@@ -132,9 +136,12 @@ public class EHCacheTokenStore implement
             Date expires = token.getExpires();
             Date current = new Date();
             long expiryTime = (expires.getTime() - current.getTime()) / 1000L;
+            if (expiryTime < 0) {
+                return 0;
+            }
             
             parsedTTL = (int)expiryTime;
-            if (expiryTime != (long)parsedTTL || parsedTTL < 0 || parsedTTL > MAX_TTL)
{
+            if (expiryTime != (long)parsedTTL || parsedTTL > MAX_TTL) {
                 // Default to configured value
                 parsedTTL = (int)ttl;
                 if (ttl != (long)parsedTTL) {

Modified: cxf/trunk/rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/MemoryTokenStore.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/MemoryTokenStore.java?rev=1309333&r1=1309332&r2=1309333&view=diff
==============================================================================
--- cxf/trunk/rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/MemoryTokenStore.java
(original)
+++ cxf/trunk/rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/MemoryTokenStore.java
Wed Apr  4 11:25:57 2012
@@ -40,14 +40,18 @@ public class MemoryTokenStore implements
     public void add(SecurityToken token) {
         if (token != null && !StringUtils.isEmpty(token.getId())) {
             CacheEntry cacheEntry = createCacheEntry(token);
-            tokens.put(token.getId(), cacheEntry);
+            if (cacheEntry != null) {
+                tokens.put(token.getId(), cacheEntry);
+            }
         }
     }
     
     public void add(String identifier, SecurityToken token) {
         if (token != null && !StringUtils.isEmpty(identifier)) {
             CacheEntry cacheEntry = createCacheEntry(token);
-            tokens.put(identifier, cacheEntry);
+            if (cacheEntry != null) {
+                tokens.put(identifier, cacheEntry);
+            }
         }
     }
     
@@ -109,7 +113,10 @@ public class MemoryTokenStore implements
             Date expires = token.getExpires();
             Date current = new Date();
             long expiryTime = expires.getTime() - current.getTime();
-            if (expiryTime < 0 || expiryTime > (MAX_TTL * 1000L)) {
+            if (expiryTime < 0) {
+                return null;
+            }
+            if (expiryTime > (MAX_TTL * 1000L)) {
                 expires.setTime(current.getTime() + (DEFAULT_TTL * 1000L));
             }
             cacheEntry = new CacheEntry(token, expires);

Added: cxf/trunk/rt/ws/security/src/test/java/org/apache/cxf/ws/security/tokenstore/EHCacheTokenStoreTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/ws/security/src/test/java/org/apache/cxf/ws/security/tokenstore/EHCacheTokenStoreTest.java?rev=1309333&view=auto
==============================================================================
--- cxf/trunk/rt/ws/security/src/test/java/org/apache/cxf/ws/security/tokenstore/EHCacheTokenStoreTest.java
(added)
+++ cxf/trunk/rt/ws/security/src/test/java/org/apache/cxf/ws/security/tokenstore/EHCacheTokenStoreTest.java
Wed Apr  4 11:25:57 2012
@@ -0,0 +1,101 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.cxf.ws.security.tokenstore;
+
+import java.util.Date;
+
+import org.apache.cxf.common.classloader.ClassLoaderUtils;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.message.MessageImpl;
+import org.apache.cxf.ws.security.SecurityConstants;
+import org.junit.BeforeClass;
+
+public class EHCacheTokenStoreTest extends org.junit.Assert {
+  
+    private static TokenStore store;
+    
+    @BeforeClass
+    public static void init() {
+        TokenStoreFactory tokenStoreFactory = new EHCacheTokenStoreFactory();
+        Message message = new MessageImpl();
+        message.put(
+            SecurityConstants.CACHE_CONFIG_FILE, 
+            ClassLoaderUtils.getResource("cxf-ehcache.xml", EHCacheTokenStoreTest.class)
+        );
+        store = tokenStoreFactory.newTokenStore(SecurityConstants.TOKEN_STORE_CACHE_INSTANCE,
message);
+    }
+    
+    // tests TokenStore apis for storing in the cache.
+    @org.junit.Test
+    public void testTokenAdd() throws Exception {
+        String key = "key";
+        SecurityToken token = new SecurityToken(key);
+        store.add(token);
+        assertEquals(token, store.getToken(key));
+        store.remove(token.getId());
+        assertNull(store.getToken(key));
+        
+        String newKey = "xyz";
+        store.add(newKey, token);
+        assertNull(store.getToken(key));
+        assertEquals(token, store.getToken(newKey));
+        store.remove(newKey);
+        assertNull(store.getToken(newKey));
+    }
+    
+    // tests TokenStore apis for storing in the cache with various expiration times
+    @org.junit.Test
+    public void testTokenAddExpiration() throws Exception {
+        SecurityToken expiredToken = new SecurityToken("expiredToken");
+        Date currentDate = new Date();
+        long currentTime = currentDate.getTime();
+        Date expiry = new Date();
+        expiry.setTime(currentTime - 5000L);
+        expiredToken.setExpires(expiry);
+        store.add(expiredToken);
+        assertTrue(store.getTokenIdentifiers().isEmpty());
+        
+        SecurityToken farFutureToken = new SecurityToken("farFuture");
+        expiry = new Date();
+        expiry.setTime(Long.MAX_VALUE);
+        farFutureToken.setExpires(expiry);
+        store.add(farFutureToken);
+        
+        assertTrue(store.getTokenIdentifiers().size() == 1);
+        store.remove(farFutureToken.getId());
+        assertTrue(store.getTokenIdentifiers().isEmpty());
+    }
+    
+    // tests TokenStore apis for removing from the cache.
+    @org.junit.Test
+    public void testTokenRemove() {
+        SecurityToken token1 = new SecurityToken("token1");
+        SecurityToken token2 = new SecurityToken("token2");
+        SecurityToken token3 = new SecurityToken("token3");
+        store.add(token1);
+        store.add(token2);
+        store.add(token3);
+        assertTrue(store.getTokenIdentifiers().size() == 3);
+        store.remove(token3.getId());
+        assertNull(store.getToken("test3"));
+        store.remove(token1.getId());
+        store.remove(token2.getId());
+        assertTrue(store.getTokenIdentifiers().size() == 0);
+    }
+}

Copied: cxf/trunk/rt/ws/security/src/test/java/org/apache/cxf/ws/security/tokenstore/MemoryTokenStoreTest.java
(from r1309068, cxf/trunk/services/sts/sts-core/src/test/java/org/apache/cxf/sts/cache/DefaultInMemoryTokenStoreTest.java)
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/ws/security/src/test/java/org/apache/cxf/ws/security/tokenstore/MemoryTokenStoreTest.java?p2=cxf/trunk/rt/ws/security/src/test/java/org/apache/cxf/ws/security/tokenstore/MemoryTokenStoreTest.java&p1=cxf/trunk/services/sts/sts-core/src/test/java/org/apache/cxf/sts/cache/DefaultInMemoryTokenStoreTest.java&r1=1309068&r2=1309333&rev=1309333&view=diff
==============================================================================
--- cxf/trunk/services/sts/sts-core/src/test/java/org/apache/cxf/sts/cache/DefaultInMemoryTokenStoreTest.java
(original)
+++ cxf/trunk/rt/ws/security/src/test/java/org/apache/cxf/ws/security/tokenstore/MemoryTokenStoreTest.java
Wed Apr  4 11:25:57 2012
@@ -16,22 +16,27 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.cxf.sts.cache;
+package org.apache.cxf.ws.security.tokenstore;
 
-import org.apache.cxf.ws.security.tokenstore.SecurityToken;
-import org.apache.cxf.ws.security.tokenstore.TokenStore;
+import java.util.Date;
+
+import org.apache.cxf.message.Message;
+import org.apache.cxf.message.MessageImpl;
+import org.apache.cxf.ws.security.SecurityConstants;
 import org.junit.BeforeClass;
 
-public class DefaultInMemoryTokenStoreTest extends org.junit.Assert {
+public class MemoryTokenStoreTest extends org.junit.Assert {
   
     private static TokenStore store;
     
     @BeforeClass
     public static void init() {
-        store = new DefaultInMemoryTokenStore();
+        TokenStoreFactory tokenStoreFactory = new MemoryTokenStoreFactory();
+        Message message = new MessageImpl();
+        store = tokenStoreFactory.newTokenStore(SecurityConstants.TOKEN_STORE_CACHE_INSTANCE,
message);
     }
     
-    // tests STSCache apis for storing in the cache.
+    // tests TokenStore apis for storing in the cache.
     @org.junit.Test
     public void testTokenAdd() throws Exception {
         String key = "key";
@@ -40,9 +45,39 @@ public class DefaultInMemoryTokenStoreTe
         assertEquals(token, store.getToken(key));
         store.remove(token.getId());
         assertNull(store.getToken(key));
+        
+        String newKey = "xyz";
+        store.add(newKey, token);
+        assertNull(store.getToken(key));
+        assertEquals(token, store.getToken(newKey));
+        store.remove(newKey);
+        assertNull(store.getToken(newKey));
+    }
+    
+    // tests TokenStore apis for storing in the cache with various expiration times
+    @org.junit.Test
+    public void testTokenAddExpiration() throws Exception {
+        SecurityToken expiredToken = new SecurityToken("expiredToken");
+        Date currentDate = new Date();
+        long currentTime = currentDate.getTime();
+        Date expiry = new Date();
+        expiry.setTime(currentTime - 5000L);
+        expiredToken.setExpires(expiry);
+        store.add(expiredToken);
+        assertTrue(store.getTokenIdentifiers().isEmpty());
+        
+        SecurityToken farFutureToken = new SecurityToken("farFuture");
+        expiry = new Date();
+        expiry.setTime(Long.MAX_VALUE);
+        farFutureToken.setExpires(expiry);
+        store.add(farFutureToken);
+        
+        assertTrue(store.getTokenIdentifiers().size() == 1);
+        store.remove(farFutureToken.getId());
+        assertTrue(store.getTokenIdentifiers().isEmpty());
     }
     
-    // tests STSCache apis for removing from the cache.
+    // tests TokenStore apis for removing from the cache.
     @org.junit.Test
     public void testTokenRemove() {
         SecurityToken token1 = new SecurityToken("token1");

Modified: cxf/trunk/services/sts/sts-core/src/main/java/org/apache/cxf/sts/cache/HazelCastTokenStore.java
URL: http://svn.apache.org/viewvc/cxf/trunk/services/sts/sts-core/src/main/java/org/apache/cxf/sts/cache/HazelCastTokenStore.java?rev=1309333&r1=1309332&r2=1309333&view=diff
==============================================================================
--- cxf/trunk/services/sts/sts-core/src/main/java/org/apache/cxf/sts/cache/HazelCastTokenStore.java
(original)
+++ cxf/trunk/services/sts/sts-core/src/main/java/org/apache/cxf/sts/cache/HazelCastTokenStore.java
Wed Apr  4 11:25:57 2012
@@ -62,14 +62,18 @@ public class HazelCastTokenStore impleme
     public void add(SecurityToken token) {
         if (token != null && !StringUtils.isEmpty(token.getId())) {
             int parsedTTL = getTTL(token);
-            cacheMap.put(token.getId(), token, parsedTTL, TimeUnit.SECONDS);
+            if (parsedTTL > 0) {
+                cacheMap.put(token.getId(), token, parsedTTL, TimeUnit.SECONDS);
+            }
         }
     }
     
     public void add(String identifier, SecurityToken token) {
         if (token != null && !StringUtils.isEmpty(identifier)) {
             int parsedTTL = getTTL(token);
-            cacheMap.put(identifier, token, parsedTTL, TimeUnit.SECONDS);
+            if (parsedTTL > 0) {
+                cacheMap.put(identifier, token, parsedTTL, TimeUnit.SECONDS);
+            }
         }
     }
     
@@ -98,9 +102,12 @@ public class HazelCastTokenStore impleme
             Date expires = token.getExpires();
             Date current = new Date();
             long expiryTime = (expires.getTime() - current.getTime()) / 1000L;
+            if (expiryTime < 0) {
+                return 0;
+            }
             
             parsedTTL = (int)expiryTime;
-            if (expiryTime != (long)parsedTTL || parsedTTL < 0 || parsedTTL > MAX_TTL)
{
+            if (expiryTime != (long)parsedTTL || parsedTTL > MAX_TTL) {
                 // Default to configured value
                 parsedTTL = (int)ttl;
                 if (ttl != (long)parsedTTL) {

Modified: cxf/trunk/services/sts/sts-core/src/test/java/org/apache/cxf/sts/cache/HazelCastTokenStoreTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/services/sts/sts-core/src/test/java/org/apache/cxf/sts/cache/HazelCastTokenStoreTest.java?rev=1309333&r1=1309332&r2=1309333&view=diff
==============================================================================
--- cxf/trunk/services/sts/sts-core/src/test/java/org/apache/cxf/sts/cache/HazelCastTokenStoreTest.java
(original)
+++ cxf/trunk/services/sts/sts-core/src/test/java/org/apache/cxf/sts/cache/HazelCastTokenStoreTest.java
Wed Apr  4 11:25:57 2012
@@ -18,6 +18,8 @@
  */
 package org.apache.cxf.sts.cache;
 
+import java.util.Date;
+
 import org.apache.cxf.ws.security.tokenstore.SecurityToken;
 import org.apache.cxf.ws.security.tokenstore.TokenStore;
 import org.junit.BeforeClass;
@@ -31,21 +33,50 @@ public class HazelCastTokenStoreTest ext
         store = new HazelCastTokenStore("default");
     }
     
-    // tests STSCache apis for storing in the cache.
+    // tests TokenStore apis for storing in the cache.
     @org.junit.Test
-    public void testCacheStore() throws Exception {
+    public void testTokenAdd() throws Exception {
         String key = "key";
         SecurityToken token = new SecurityToken(key);
         store.add(token);
-        SecurityToken cachedToken = store.getToken(key);
-        assertEquals(token.getId(), cachedToken.getId());
+        assertEquals(token, store.getToken(key));
         store.remove(token.getId());
         assertNull(store.getToken(key));
+        
+        String newKey = "xyz";
+        store.add(newKey, token);
+        assertNull(store.getToken(key));
+        assertEquals(token, store.getToken(newKey));
+        store.remove(newKey);
+        assertNull(store.getToken(newKey));
+    }
+    
+    // tests TokenStore apis for storing in the cache with various expiration times
+    @org.junit.Test
+    public void testTokenAddExpiration() throws Exception {
+        SecurityToken expiredToken = new SecurityToken("expiredToken");
+        Date currentDate = new Date();
+        long currentTime = currentDate.getTime();
+        Date expiry = new Date();
+        expiry.setTime(currentTime - 5000L);
+        expiredToken.setExpires(expiry);
+        store.add(expiredToken);
+        assertTrue(store.getTokenIdentifiers().isEmpty());
+        
+        SecurityToken farFutureToken = new SecurityToken("farFuture");
+        expiry = new Date();
+        expiry.setTime(Long.MAX_VALUE);
+        farFutureToken.setExpires(expiry);
+        store.add(farFutureToken);
+        
+        assertTrue(store.getTokenIdentifiers().size() == 1);
+        store.remove(farFutureToken.getId());
+        assertTrue(store.getTokenIdentifiers().isEmpty());
     }
     
-    // tests STSCache apis for removing from the cache.
+    // tests TokenStore apis for removing from the cache.
     @org.junit.Test
-    public void testCacheRemove() {
+    public void testTokenRemove() {
         SecurityToken token1 = new SecurityToken("token1");
         SecurityToken token2 = new SecurityToken("token2");
         SecurityToken token3 = new SecurityToken("token3");



Mime
View raw message