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.util.Iterator;
025: import java.util.Set;
026: import javax.security.auth.login.Configuration;
027: import javax.security.auth.login.LoginContext;
028: import javax.security.auth.login.LoginException;
029: import javax.security.auth.Subject;
030:
031: import junit.framework.TestCase;
032:
033: import org.jboss.security.auth.login.XMLLoginConfigImpl;
034: import org.jboss.security.SimplePrincipal;
035:
036: public class LoginContextTestCase extends TestCase {
037:
038: public LoginContextTestCase(String name) {
039: super (name);
040: }
041:
042: protected void setUp() throws Exception {
043: System.setOut(System.err);
044: XMLLoginConfigImpl config = new XMLLoginConfigImpl();
045: config.setConfigResource("login-config.xml");
046: config.loadConfig();
047: Configuration.setConfiguration(config);
048: }
049:
050: private void validateSuccessfulLogin(LoginContext lc)
051: throws LoginException {
052: Subject subject = lc.getSubject();
053: assertTrue("case5 subject != null", subject != null);
054: boolean hasGuest = subject.getPrincipals().contains(
055: new SimplePrincipal("guest"));
056: assertTrue("subject has guest principal", hasGuest);
057: lc.logout();
058: hasGuest = subject.getPrincipals().contains(
059: new SimplePrincipal("guest"));
060: assertTrue("subject has guest principal", hasGuest == false);
061: Set publicCreds = subject.getPublicCredentials();
062: assertTrue("public creds has 'A public credential'",
063: publicCreds.contains("A public credential"));
064: Set privateCreds = subject.getPrivateCredentials();
065: assertTrue("private creds has 'A private credential'",
066: privateCreds.contains("A private credential"));
067: Iterator iter = privateCreds.iterator();
068: int count = 0;
069: while (iter.hasNext()) {
070: iter.next();
071: count++;
072: }
073: assertTrue("private creds has 1 entry", count == 1);
074: }
075:
076: public void testCase1() throws Exception {
077: LoginContext lc = new LoginContext("case1");
078: lc.login();
079: validateSuccessfulLogin(lc);
080: }
081:
082: public void testCase2() throws Exception {
083: LoginContext lc = new LoginContext("case2");
084: lc.login();
085: validateSuccessfulLogin(lc);
086: }
087:
088: public void testCase3() throws Exception {
089: LoginContext lc = new LoginContext("case3");
090: try {
091: lc.login();
092: fail("LoginContext.login3 did not thrown an exception");
093: } catch (LoginException e) {
094: e.printStackTrace();
095: }
096: }
097:
098: /** This should fail because no login module succeeds
099: *
100: * @throws Exception
101: */
102: public void testCase4() throws Exception {
103: LoginContext lc = new LoginContext("case4");
104: try {
105: lc.login();
106: fail("LoginContext.login4 did not thrown an exception");
107: } catch (LoginException e) {
108: e.printStackTrace();
109: }
110: }
111:
112: public void testCase5() throws Exception {
113: LoginContext lc = new LoginContext("case5");
114: lc.login();
115: validateSuccessfulLogin(lc);
116: }
117:
118: public void testCase6() throws Exception {
119: LoginContext lc = new LoginContext("case6");
120: lc.login();
121: validateSuccessfulLogin(lc);
122: }
123:
124: public void testCase7() throws Exception {
125: LoginContext lc = new LoginContext("case7");
126: lc.login();
127: validateSuccessfulLogin(lc);
128: }
129:
130: public void testCase8() throws Exception {
131: LoginContext lc = new LoginContext("case8");
132: try {
133: lc.login();
134: fail("LoginContext.login8 did not thrown an exception");
135: } catch (LoginException e) {
136: e.printStackTrace();
137: }
138: }
139:
140: public void testCase9() throws Exception {
141: LoginContext lc = new LoginContext("case9");
142: lc.login();
143: validateSuccessfulLogin(lc);
144: }
145:
146: public void testCase10() throws Exception {
147: LoginContext lc = new LoginContext("case10");
148: try {
149: lc.login();
150: fail("LoginContext.login10 did not thrown an exception");
151: } catch (LoginException e) {
152: e.printStackTrace();
153: }
154: }
155:
156: public void testCase11() throws Exception {
157: LoginContext lc = new LoginContext("case11");
158: lc.login();
159: validateSuccessfulLogin(lc);
160: }
161:
162: public void testCase12() throws Exception {
163: LoginContext lc = new LoginContext("case12");
164: lc.login();
165: validateSuccessfulLogin(lc);
166: }
167:
168: public void testCase13() throws Exception {
169: LoginContext lc = new LoginContext("case13");
170: try {
171: lc.login();
172: fail("LoginContext.login13 did not thrown an exception");
173: } catch (LoginException e) {
174: e.printStackTrace();
175: }
176: }
177:
178: public void testCase14() throws Exception {
179: LoginContext lc = new LoginContext("case14");
180: try {
181: lc.login();
182: fail("LoginContext.login14 did not thrown an exception");
183: } catch (LoginException e) {
184: e.printStackTrace();
185: }
186: }
187:
188: public void testCase15() throws Exception {
189: LoginContext lc = new LoginContext("case15");
190: try {
191: lc.login();
192: fail("LoginContext.login15 did not thrown an exception");
193: } catch (LoginException e) {
194: e.printStackTrace();
195: }
196: }
197:
198: public void testCase16() throws Exception {
199: LoginContext lc = new LoginContext("case16");
200: lc.login();
201: validateSuccessfulLogin(lc);
202: }
203:
204: public void testCase17() throws Exception {
205: LoginContext lc = new LoginContext("case17");
206: lc.login();
207: validateSuccessfulLogin(lc);
208: }
209:
210: public void testCase18() throws Exception {
211: LoginContext lc = new LoginContext("case18");
212: try {
213: lc.login();
214: fail("LoginContext.login18 did not thrown an exception");
215: } catch (LoginException e) {
216: e.printStackTrace();
217: }
218: }
219:
220: public void testCase19() throws Exception {
221: LoginContext lc = new LoginContext("case19");
222: try {
223: lc.login();
224: fail("LoginContext.login19 did not thrown an exception");
225: } catch (LoginException e) {
226: e.printStackTrace();
227: }
228: }
229:
230: public void testCase20() throws Exception {
231: LoginContext lc = new LoginContext("case20");
232: try {
233: lc.login();
234: fail("LoginContext.login20 did not thrown an exception");
235: } catch (LoginException e) {
236: e.printStackTrace();
237: }
238: }
239:
240: public void testCase21() throws Exception {
241: LoginContext lc = new LoginContext("case21");
242: try {
243: lc.login();
244: } catch (LoginException e) {
245: e.printStackTrace();
246: }
247: Subject subject = lc.getSubject();
248: assertTrue("case21 subject == null", subject == null);
249: }
250:
251: public void testCase22() throws Exception {
252: LoginContext lc = new LoginContext("case22");
253: try {
254: lc.login();
255: fail("LoginContext.login22 did not thrown an exception");
256: } catch (LoginException e) {
257: e.printStackTrace();
258: }
259: }
260:
261: }
|