001: /*
002: jGuard is a security framework based on top of jaas (java authentication and authorization security).
003: it is written for web applications, to resolve simply, access control problems.
004: version $Name: $
005: http://sourceforge.net/projects/jguard/
006:
007: Copyright (C) 2004 Charles GAY
008:
009: This library is free software; you can redistribute it and/or
010: modify it under the terms of the GNU Lesser General Public
011: License as published by the Free Software Foundation; either
012: version 2.1 of the License, or (at your option) any later version.
013:
014: This library is distributed in the hope that it will be useful,
015: but WITHOUT ANY WARRANTY; without even the implied warranty of
016: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: Lesser General Public License for more details.
018:
019: You should have received a copy of the GNU Lesser General Public
020: License along with this library; if not, write to the Free Software
021: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022:
023:
024: jGuard project home page:
025: http://sourceforge.net/projects/jguard/
026:
027: */
028: package net.sf.jguard.core.authentication.configuration;
029:
030: import java.util.ArrayList;
031: import java.util.HashMap;
032: import java.util.List;
033: import java.util.Map;
034:
035: import javax.security.auth.login.AppConfigurationEntry;
036: import javax.security.auth.login.AppConfigurationEntry.LoginModuleControlFlag;
037:
038: import junit.framework.TestCase;
039: import net.sf.jguard.core.CoreConstants;
040:
041: public class JGuardConfigurationTest extends TestCase {
042:
043: /*
044: * Generates a mock list of Login Modules
045: */
046: private List generateMockLoginModuleList() {
047: List loginModules = new ArrayList();
048:
049: Map first = new HashMap();
050: first
051: .put(CoreConstants.NAME,
052: "net.sf.jguard.authentication.loginmodules.XmlLoginModule");
053: first.put(CoreConstants.FLAG, "REQUIRED");
054: Map firstOptions = new HashMap();
055: firstOptions.put("debug", "false");
056: firstOptions.put("fileLocation",
057: "WEB-INF/conf/jGuard/jGuardUsersPrincipals.xml");
058: firstOptions
059: .put("authenticationManager",
060: "net.sf.jguard.authentication.XmlAuthenticationManager");
061: first.put(CoreConstants.LOGIN_MODULE_OPTIONS, firstOptions);
062: loginModules.add(first);
063:
064: Map second = new HashMap();
065: second
066: .put(CoreConstants.NAME,
067: "net.sf.jguard.authentication.loginmodules.PostgreSQLLoginModule");
068: second.put(CoreConstants.FLAG, "OPTIONAL");
069: Map secondOptions = new HashMap();
070: secondOptions.put("authenticationUrl",
071: "jdbc:postgresql://127.0.0.1:5434/test");
072: secondOptions.put("authenticationLogin", "testLogin");
073: secondOptions.put("authenticationPassword", "testPassword");
074: secondOptions.put("authenticationDriver",
075: "org.postgresql.Driver");
076: second.put(CoreConstants.LOGIN_MODULE_OPTIONS, secondOptions);
077: loginModules.add(second);
078:
079: return loginModules;
080: }
081:
082: /*
083: * Tests interaction between ConfigurationHelper and JGuardConfiguration
084: */
085: public void testGetApplicationEntry() {
086: Map authenticationMap = new HashMap();
087: authenticationMap.put(CoreConstants.DEBUG, "true");
088:
089: authenticationMap
090: .put(CoreConstants.INCLUDE_OLD_CONFIG, "false");
091:
092: List loginModules = generateMockLoginModuleList();
093: authenticationMap
094: .put(CoreConstants.LOGIN_MODULES, loginModules);
095:
096: // Application name
097: String appName = "test";
098: JGuardConfiguration jGuardConf = new JGuardConfiguration();
099: ConfigurationHelper.addConfigurationEntryForWebapp(jGuardConf,
100: appName, authenticationMap, false);
101: //JGuardConfiguration config = (JGuardConfiguration) Configuration.getConfiguration();
102: AppConfigurationEntry[] entries = jGuardConf
103: .getAppConfigurationEntry("test");
104:
105: // check that both loginModules have been inserted in the configuration corresponding
106: // to the application called 'test'
107: assertEquals(entries.length, 2);
108:
109: // XML Mock Module
110: AppConfigurationEntry xmlEntry = entries[0];
111:
112: // Values for login module settings
113: assertEquals(xmlEntry.getLoginModuleName(),
114: "net.sf.jguard.authentication.loginmodules.XmlLoginModule");
115: assertEquals(xmlEntry.getControlFlag(),
116: LoginModuleControlFlag.REQUIRED);
117: Map xmlEntryOptions = xmlEntry.getOptions();
118: assertEquals((String) xmlEntryOptions.get("debug"), "false");
119: assertEquals((String) xmlEntryOptions.get("fileLocation"),
120: "WEB-INF/conf/jGuard/jGuardUsersPrincipals.xml");
121: assertEquals((String) xmlEntryOptions
122: .get("authenticationManager"),
123: "net.sf.jguard.authentication.XmlAuthenticationManager");
124:
125: // PostgreSQL Mock Module
126: AppConfigurationEntry pgEntry = entries[1];
127:
128: // Values for login module settings
129: assertEquals(pgEntry.getLoginModuleName(),
130: "net.sf.jguard.authentication.loginmodules.PostgreSQLLoginModule");
131: assertEquals(pgEntry.getControlFlag(),
132: LoginModuleControlFlag.OPTIONAL);
133: Map pgEntryOptions = pgEntry.getOptions();
134: assertEquals((String) pgEntryOptions.get("authenticationUrl"),
135: "jdbc:postgresql://127.0.0.1:5434/test");
136: assertEquals(
137: (String) pgEntryOptions.get("authenticationLogin"),
138: "testLogin");
139: assertEquals((String) pgEntryOptions
140: .get("authenticationPassword"), "testPassword");
141: assertEquals((String) pgEntryOptions
142: .get("authenticationDriver"), "org.postgresql.Driver");
143:
144: }
145:
146: }
|