1 23 package com.sun.enterprise.management.config; 24 25 import java.util.Map ; 26 import java.util.HashMap ; 27 import java.util.Set ; 28 import java.util.HashSet ; 29 import java.util.List ; 30 import java.util.ArrayList ; 31 32 import com.sun.appserv.management.config.AMXConfig; 33 import com.sun.appserv.management.config.AuthRealmConfig; 34 import com.sun.appserv.management.config.SecurityServiceConfig; 35 import static com.sun.appserv.management.config.AuthRealmConfig.*; 36 37 import com.sun.appserv.management.base.Container; 38 import com.sun.appserv.management.base.XTypes; 39 import com.sun.appserv.management.base.Util; 40 import com.sun.appserv.management.util.misc.GSetUtil; 41 import com.sun.appserv.management.util.misc.CollectionUtil; 42 43 44 46 public final class AuthRealmConfigTest extends ConfigMgrTestBase 47 { 48 public 49 AuthRealmConfigTest() 50 { 51 if ( checkNotOffline( "ensureDefaultInstance" ) ) 52 { 53 ensureDefaultInstance( getConfigConfig().getSecurityServiceConfig() ); 54 } 55 } 56 57 private static boolean WARNED_TESTABLE = false; 58 59 private static final String TEMPLATE_PREFIX = "${"; 60 61 private synchronized Map <String ,AuthRealmConfig> 62 getTestableAuthRealms() 63 { 64 final Map <String ,AuthRealmConfig> m = 65 getConfigConfig().getSecurityServiceConfig().getAuthRealmConfigMap(); 66 67 final Map <String ,AuthRealmConfig> std = new HashMap <String ,AuthRealmConfig>(); 68 final List <String > warnings = new ArrayList <String >(); 69 for( final String name : m.keySet() ) 70 { 71 final AuthRealmConfig c = m.get( name ); 72 73 if ( AuthRealmConfig.DEFAULT_REALM_CLASSNAME.equals( c.getClassname() ) ) 74 { 75 try 76 { 77 final String file = c.getPropertyValue( "file" ); 78 if ( file == null ) 79 { 80 warnings.add( "Realm " + name + 81 " does not have a 'file' property (test skipped)" ); 82 } 83 else if ( file.indexOf( TEMPLATE_PREFIX ) >= 0 ) 84 { 85 warnings.add( "Realm " + name + 86 " uses a ${...} name, not yet supported (test skipped)" ); 87 } 88 else 89 { 90 std.put( c.getName(), c ); 91 } 92 } 93 catch( Exception e ) 94 { 95 } 96 } 97 } 98 99 if ( ! WARNED_TESTABLE ) 100 { 101 WARNED_TESTABLE = true; 102 warning( NEWLINE + CollectionUtil.toString( warnings, NEWLINE ) + NEWLINE + 103 "Realms which WILL be tested: {" + CollectionUtil.toString( m.keySet() ) + "}"); 104 } 105 106 return std; 107 } 108 109 public synchronized void 110 testGetters() 111 { 112 final Map <String ,AuthRealmConfig> arcMap = getTestableAuthRealms(); 113 114 for( final AuthRealmConfig ar : arcMap.values() ) 115 { 116 ar.getName(); 117 final String classname = ar.getClassname(); 118 if ( classname != null ) 119 { 120 ar.setClassname( classname ); 121 } 122 } 123 } 124 125 private boolean 126 userExists( final AuthRealmConfig config, final String user ) 127 { 128 return GSetUtil.newStringSet( (String [])config.getUserNames() ).contains( user ); 129 } 130 131 public synchronized void 132 testAddRemoveUpdateUser() 133 throws Exception 134 { 135 final Map <String ,AuthRealmConfig> arcMap = getTestableAuthRealms(); 136 137 final String USER = "test"; 138 139 final Set <AuthRealmConfig> failures = new HashSet <AuthRealmConfig>(); 140 141 for( final AuthRealmConfig ar : arcMap.values() ) 142 { 143 145 try 146 { 147 ar.getUserNames(); 148 149 if ( userExists( ar, USER ) ) 150 { 151 ar.removeUser( USER ); 152 } 153 154 ar.addUser( USER, "foo-pwd", null ); 155 assert( userExists( ar, USER ) ); 156 ar.updateUser( USER, "foo-pwd2", null ); 157 assert( userExists( ar, USER ) ); 158 ar.removeUser( USER ); 159 assert( ! userExists( ar, USER ) ); 160 } 162 catch( Exception e ) 163 { 164 trace( "" ); 165 trace( "" ); 166 trace( "FAILURE FOR: " + ar.getName() ); 167 failures.add( ar ); 169 } 170 } 171 172 if ( failures.size() != 0 ) 173 { 174 final Set <String > names = Util.getNames( failures ); 175 176 warning( "testAddRemoveUpdateUser failed on the following realms: " + 177 CollectionUtil.toString( names ) ); 178 assert( false ); 179 } 180 181 } 182 183 public synchronized void 184 testGetGroupNames() 185 { 186 final Map <String ,AuthRealmConfig> arcMap = getTestableAuthRealms(); 187 188 for( final AuthRealmConfig ar : arcMap.values() ) 189 { 190 ar.getGroupNames(); 191 } 192 } 193 194 public synchronized void 195 testGetUserGroupNames() 196 { 197 final Map <String ,AuthRealmConfig> arcMap = getTestableAuthRealms(); 198 199 for( final AuthRealmConfig ar : arcMap.values() ) 200 { 201 final String [] users = ar.getUserNames(); 202 for( final String user : users ) 203 { 204 ar.getUserGroupNames( user ); 205 } 206 } 207 } 208 209 210 public static String 211 getDefaultInstanceName() 212 { 213 return getDefaultInstanceName( "AuthRealmConfig" ); 214 } 215 216 public static AuthRealmConfig 217 ensureDefaultInstance( final SecurityServiceConfig securityServiceConfig ) 218 { 219 AuthRealmConfig result = 220 securityServiceConfig.getAuthRealmConfigMap().get( getDefaultInstanceName() ); 221 222 if ( result == null ) 223 { 224 result = createInstance( securityServiceConfig, 225 getDefaultInstanceName(), 226 DEFAULT_REALM_CLASSNAME, 227 KEY_FILE_PREFIX + "default-instance-test" ); 228 } 229 230 return result; 231 } 232 233 public static AuthRealmConfig 234 createInstance( 235 final SecurityServiceConfig securityServiceConfig, 236 final String name, 237 final String classname, 238 final String keyFile ) 239 { 240 final Map <String ,String > options = new HashMap <String ,String >(); 241 242 options.put( KEY_FILE_PROPERTY_KEY, keyFile ); 243 options.put( JAAS_CONTEXT_PROPERTY_KEY, "dummy-jaas-context-value" ); 244 245 return securityServiceConfig.createAuthRealmConfig( name, classname, options); 246 } 247 248 249 250 protected Container 251 getProgenyContainer() 252 { 253 return getConfigConfig().getSecurityServiceConfig(); 254 } 255 256 protected String 257 getProgenyJ2EEType() 258 { 259 return XTypes.AUTH_REALM_CONFIG; 260 } 261 262 263 protected void 264 removeProgeny( final String name ) 265 { 266 getConfigConfig().getSecurityServiceConfig().removeAuthRealmConfig( name ); 267 } 268 269 270 protected final AMXConfig 271 createProgeny(final String name, final Map <String ,String > options ) 272 { 273 if ( name.indexOf( "Illegal" ) >= 0 ) 274 { 275 throw new IllegalArgumentException (); 278 } 279 280 return createInstance( getConfigConfig().getSecurityServiceConfig(), 281 name, DEFAULT_REALM_CLASSNAME, name + "-keyfile" ); 282 } 283 } 284 285 286 | Popular Tags |