001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test;
023:
024: import java.io.InputStreamReader;
025: import java.net.URL;
026: import javax.security.auth.login.AppConfigurationEntry;
027: import javax.security.auth.login.AppConfigurationEntry.LoginModuleControlFlag;
028:
029: import junit.framework.TestCase;
030:
031: import org.jboss.security.auth.login.SunConfigParser;
032: import org.jboss.security.auth.login.XMLLoginConfigImpl;
033:
034: /** Tests of the Sun login configuration file format parser
035: *
036: * @author Scott.Stark@jboss.org
037: * @version $Revision: 37390 $
038: */
039: public class SunConfigParserTestCase extends TestCase {
040:
041: public SunConfigParserTestCase(String name) {
042: super (name);
043: }
044:
045: /** Test the Sun config file parser directly.
046: *
047: * @throws Exception
048: */
049: public void testParser() throws Exception {
050: XMLLoginConfigImpl config = new XMLLoginConfigImpl();
051: ClassLoader loader = Thread.currentThread()
052: .getContextClassLoader();
053: URL configURL = loader.getResource("login-config.conf");
054: InputStreamReader configFile = new InputStreamReader(configURL
055: .openStream());
056: SunConfigParser.doParse(configFile, config, true);
057:
058: AppConfigurationEntry[] entry = config
059: .getAppConfigurationEntry("case1");
060: assertTrue("case1 entry != null", entry != null);
061: assertTrue("case1.length == 2", entry.length == 2);
062: assertTrue("case1[0].module == org.jboss.test.TestLoginModule",
063: entry[0].getLoginModuleName().equals(
064: "org.jboss.test.TestLoginModule"));
065: assertTrue("case1[0].flag == required", entry[0]
066: .getControlFlag() == LoginModuleControlFlag.REQUIRED);
067: assertTrue("case1[0].option(name) == 1.1", entry[0]
068: .getOptions().get("name").equals("1.1"));
069: assertTrue("case1[0].option(succeed) == true", entry[0]
070: .getOptions().get("succeed").equals("true"));
071: assertTrue("case1[0].option(throwEx) == false", entry[0]
072: .getOptions().get("throwEx").equals("false"));
073:
074: entry = config.getAppConfigurationEntry("case2");
075: assertTrue("case2 entry != null", entry != null);
076: assertTrue("case2.length == 2", entry.length == 2);
077: assertTrue("case2[0].module = org.jboss.test.TestLoginModule",
078: entry[0].getLoginModuleName().equals(
079: "org.jboss.test.TestLoginModule"));
080: assertTrue("case2[0].flag == optional", entry[0]
081: .getControlFlag() == LoginModuleControlFlag.OPTIONAL);
082: assertTrue("case2[1].option(name) == 2.2", entry[1]
083: .getOptions().get("name").equals("2.2"));
084: assertTrue("case2[1].option(succeed) == false", entry[1]
085: .getOptions().get("succeed").equals("false"));
086: assertTrue("case2[1].option(throwEx) == true", entry[1]
087: .getOptions().get("throwEx").equals("true"));
088: }
089:
090: /** Test the Sun config file parser by creating a XMLLoginConfig with a
091: * URL pointing to a Sun format config file.
092: *
093: * @throws Exception
094: */
095: public void testSunLoginConfig() throws Exception {
096: XMLLoginConfigImpl config = new XMLLoginConfigImpl();
097: ClassLoader loader = Thread.currentThread()
098: .getContextClassLoader();
099: URL configURL = loader.getResource("login-config.conf");
100: config.setConfigURL(configURL);
101: config.loadConfig();
102:
103: AppConfigurationEntry[] entry = config
104: .getAppConfigurationEntry("case1");
105: assertTrue("case1 entry != null", entry != null);
106: assertTrue("case1.length == 2", entry.length == 2);
107: assertTrue("case1[0].module == org.jboss.test.TestLoginModule",
108: entry[0].getLoginModuleName().equals(
109: "org.jboss.test.TestLoginModule"));
110: assertTrue("case1[0].flag == required", entry[0]
111: .getControlFlag() == LoginModuleControlFlag.REQUIRED);
112: assertTrue("case1[0].option(name) == 1.1", entry[0]
113: .getOptions().get("name").equals("1.1"));
114: assertTrue("case1[0].option(succeed) == true", entry[0]
115: .getOptions().get("succeed").equals("true"));
116: assertTrue("case1[0].option(throwEx) == false", entry[0]
117: .getOptions().get("throwEx").equals("false"));
118:
119: entry = config.getAppConfigurationEntry("case2");
120: assertTrue("case2 entry != null", entry != null);
121: assertTrue("case2.length == 2", entry.length == 2);
122: assertTrue("case2[0].module = org.jboss.test.TestLoginModule",
123: entry[0].getLoginModuleName().equals(
124: "org.jboss.test.TestLoginModule"));
125: assertTrue("case2[0].flag == optional", entry[0]
126: .getControlFlag() == LoginModuleControlFlag.OPTIONAL);
127: assertTrue("case2[1].option(name) == 2.2", entry[1]
128: .getOptions().get("name").equals("2.2"));
129: assertTrue("case2[1].option(succeed) == false", entry[1]
130: .getOptions().get("succeed").equals("false"));
131: assertTrue("case2[1].option(throwEx) == true", entry[1]
132: .getOptions().get("throwEx").equals("true"));
133: }
134: }
|