|
| 1 | +package org.springframework.security.acls.jdbc; |
| 2 | + |
| 3 | +import org.junit.After; |
| 4 | +import org.junit.AfterClass; |
| 5 | +import org.junit.BeforeClass; |
| 6 | +import org.junit.Test; |
| 7 | +import org.springframework.cache.Cache; |
| 8 | +import org.springframework.cache.CacheManager; |
| 9 | +import org.springframework.cache.concurrent.ConcurrentMapCacheManager; |
| 10 | +import org.springframework.security.acls.domain.*; |
| 11 | +import org.springframework.security.acls.model.MutableAcl; |
| 12 | +import org.springframework.security.acls.model.ObjectIdentity; |
| 13 | +import org.springframework.security.acls.model.PermissionGrantingStrategy; |
| 14 | +import org.springframework.security.authentication.TestingAuthenticationToken; |
| 15 | +import org.springframework.security.core.Authentication; |
| 16 | +import org.springframework.security.core.authority.SimpleGrantedAuthority; |
| 17 | +import org.springframework.security.core.context.SecurityContextHolder; |
| 18 | +import org.springframework.security.util.FieldUtils; |
| 19 | + |
| 20 | +import java.io.*; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +import static org.junit.Assert.*; |
| 24 | + |
| 25 | +/** |
| 26 | + * Tests {@link org.springframework.security.acls.domain.EhCacheBasedAclCache} |
| 27 | + * |
| 28 | + * @author Andrei Stefan |
| 29 | + */ |
| 30 | +public class SpringCacheBasedAclCacheTests { |
| 31 | + private static final String TARGET_CLASS = "org.springframework.security.acls.TargetObject"; |
| 32 | + |
| 33 | + private static CacheManager cacheManager; |
| 34 | + |
| 35 | + @BeforeClass |
| 36 | + public static void initCacheManaer() { |
| 37 | + cacheManager = new ConcurrentMapCacheManager(); |
| 38 | + // Use disk caching immediately (to test for serialization issue reported in SEC-527) |
| 39 | + cacheManager.getCache("springcasebasedacltests"); |
| 40 | + } |
| 41 | + |
| 42 | + @After |
| 43 | + public void clearContext() { |
| 44 | + SecurityContextHolder.clearContext(); |
| 45 | + } |
| 46 | + |
| 47 | + private Cache getCache() { |
| 48 | + Cache cache = cacheManager.getCache("springcasebasedacltests"); |
| 49 | + cache.clear(); |
| 50 | + return cache; |
| 51 | + } |
| 52 | + |
| 53 | + @Test(expected=IllegalArgumentException.class) |
| 54 | + public void constructorRejectsNullParameters() throws Exception { |
| 55 | + new SpringCacheBasedAclCache(null, null, null); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void cacheOperationsAclWithoutParent() throws Exception { |
| 60 | + Cache cache = getCache(); |
| 61 | + Map realCache = (Map) cache.getNativeCache(); |
| 62 | + ObjectIdentity identity = new ObjectIdentityImpl(TARGET_CLASS, Long.valueOf(100)); |
| 63 | + AclAuthorizationStrategy aclAuthorizationStrategy = new AclAuthorizationStrategyImpl( |
| 64 | + new SimpleGrantedAuthority("ROLE_OWNERSHIP"), new SimpleGrantedAuthority("ROLE_AUDITING"), |
| 65 | + new SimpleGrantedAuthority("ROLE_GENERAL")); |
| 66 | + AuditLogger auditLogger = new ConsoleAuditLogger(); |
| 67 | + |
| 68 | + PermissionGrantingStrategy permissionGrantingStrategy = new DefaultPermissionGrantingStrategy(auditLogger); |
| 69 | + SpringCacheBasedAclCache myCache = new SpringCacheBasedAclCache(cache, permissionGrantingStrategy, aclAuthorizationStrategy); |
| 70 | + MutableAcl acl = new AclImpl(identity, Long.valueOf(1), aclAuthorizationStrategy, auditLogger); |
| 71 | + |
| 72 | + assertEquals(0, realCache.size()); |
| 73 | + myCache.putInCache(acl); |
| 74 | + |
| 75 | + // Check we can get from cache the same objects we put in |
| 76 | + assertEquals(myCache.getFromCache(Long.valueOf(1)), acl); |
| 77 | + assertEquals(myCache.getFromCache(identity), acl); |
| 78 | + |
| 79 | + // Put another object in cache |
| 80 | + ObjectIdentity identity2 = new ObjectIdentityImpl(TARGET_CLASS, Long.valueOf(101)); |
| 81 | + MutableAcl acl2 = new AclImpl(identity2, Long.valueOf(2), aclAuthorizationStrategy, new ConsoleAuditLogger()); |
| 82 | + |
| 83 | + myCache.putInCache(acl2); |
| 84 | + |
| 85 | + // Try to evict an entry that doesn't exist |
| 86 | + myCache.evictFromCache(Long.valueOf(3)); |
| 87 | + myCache.evictFromCache(new ObjectIdentityImpl(TARGET_CLASS, Long.valueOf(102))); |
| 88 | + assertEquals(realCache.size(), 4); |
| 89 | + |
| 90 | + myCache.evictFromCache(Long.valueOf(1)); |
| 91 | + assertEquals(realCache.size(), 2); |
| 92 | + |
| 93 | + // Check the second object inserted |
| 94 | + assertEquals(myCache.getFromCache(Long.valueOf(2)), acl2); |
| 95 | + assertEquals(myCache.getFromCache(identity2), acl2); |
| 96 | + |
| 97 | + myCache.evictFromCache(identity2); |
| 98 | + assertEquals(realCache.size(), 0); |
| 99 | + } |
| 100 | + |
| 101 | + @SuppressWarnings("unchecked") |
| 102 | + @Test |
| 103 | + public void cacheOperationsAclWithParent() throws Exception { |
| 104 | + Cache cache = getCache(); |
| 105 | + Map realCache = (Map) cache.getNativeCache(); |
| 106 | + |
| 107 | + Authentication auth = new TestingAuthenticationToken("user", "password", "ROLE_GENERAL"); |
| 108 | + auth.setAuthenticated(true); |
| 109 | + SecurityContextHolder.getContext().setAuthentication(auth); |
| 110 | + |
| 111 | + ObjectIdentity identity = new ObjectIdentityImpl(TARGET_CLASS, Long.valueOf(1)); |
| 112 | + ObjectIdentity identityParent = new ObjectIdentityImpl(TARGET_CLASS, Long.valueOf(2)); |
| 113 | + AclAuthorizationStrategy aclAuthorizationStrategy = new AclAuthorizationStrategyImpl( |
| 114 | + new SimpleGrantedAuthority("ROLE_OWNERSHIP"), new SimpleGrantedAuthority("ROLE_AUDITING"), |
| 115 | + new SimpleGrantedAuthority("ROLE_GENERAL")); |
| 116 | + AuditLogger auditLogger = new ConsoleAuditLogger(); |
| 117 | + |
| 118 | + PermissionGrantingStrategy permissionGrantingStrategy = new DefaultPermissionGrantingStrategy(auditLogger); |
| 119 | + SpringCacheBasedAclCache myCache = new SpringCacheBasedAclCache(cache, permissionGrantingStrategy, aclAuthorizationStrategy); |
| 120 | + |
| 121 | + MutableAcl acl = new AclImpl(identity, Long.valueOf(1), aclAuthorizationStrategy, auditLogger); |
| 122 | + MutableAcl parentAcl = new AclImpl(identityParent, Long.valueOf(2), aclAuthorizationStrategy, auditLogger); |
| 123 | + |
| 124 | + acl.setParent(parentAcl); |
| 125 | + |
| 126 | + assertEquals(0, realCache.size()); |
| 127 | + myCache.putInCache(acl); |
| 128 | + assertEquals(realCache.size(), 4); |
| 129 | + |
| 130 | + // Check we can get from cache the same objects we put in |
| 131 | + AclImpl aclFromCache = (AclImpl) myCache.getFromCache(Long.valueOf(1)); |
| 132 | + assertEquals(acl, aclFromCache); |
| 133 | + // SEC-951 check transient fields are set on parent |
| 134 | + assertNotNull(FieldUtils.getFieldValue(aclFromCache.getParentAcl(), "aclAuthorizationStrategy")); |
| 135 | + assertNotNull(FieldUtils.getFieldValue(aclFromCache.getParentAcl(), "permissionGrantingStrategy")); |
| 136 | + assertEquals(acl, myCache.getFromCache(identity)); |
| 137 | + assertNotNull(FieldUtils.getFieldValue(aclFromCache, "aclAuthorizationStrategy")); |
| 138 | + AclImpl parentAclFromCache = (AclImpl) myCache.getFromCache(Long.valueOf(2)); |
| 139 | + assertEquals(parentAcl, parentAclFromCache); |
| 140 | + assertNotNull(FieldUtils.getFieldValue(parentAclFromCache, "aclAuthorizationStrategy")); |
| 141 | + assertEquals(parentAcl, myCache.getFromCache(identityParent)); |
| 142 | + } |
| 143 | + |
| 144 | + //~ Inner Classes ================================================================================================== |
| 145 | + |
| 146 | + private class MockCache implements Cache { |
| 147 | + |
| 148 | + @Override |
| 149 | + public String getName() { |
| 150 | + return "mockcache"; |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public Object getNativeCache() { |
| 155 | + return null; |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public ValueWrapper get(Object key) { |
| 160 | + return null; |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + public void put(Object key, Object value) {} |
| 165 | + |
| 166 | + @Override |
| 167 | + public void evict(Object key) {} |
| 168 | + |
| 169 | + @Override |
| 170 | + public void clear() {} |
| 171 | + } |
| 172 | +} |
0 commit comments