001: /**
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package org.apache.openejb.client;
018:
019: import junit.framework.TestCase;
020:
021: import javax.naming.spi.InitialContextFactory;
022: import javax.naming.spi.NamingManager;
023: import javax.naming.spi.InitialContextFactoryBuilder;
024: import javax.naming.Context;
025: import javax.naming.Name;
026: import javax.naming.NamingException;
027: import javax.naming.NameClassPair;
028: import javax.naming.NamingEnumeration;
029: import javax.naming.Binding;
030: import javax.naming.NameParser;
031: import javax.naming.NameNotFoundException;
032: import javax.security.auth.Subject;
033: import javax.security.auth.login.FailedLoginException;
034: import java.util.Hashtable;
035: import java.util.Map;
036: import java.util.TreeMap;
037: import java.security.AccessController;
038:
039: public class MainTest extends TestCase {
040: static {
041: try {
042: NamingManager
043: .setInitialContextFactoryBuilder(new MockContextFactoryBuilder());
044: } catch (NamingException e) {
045: }
046: }
047:
048: public static Map<String, Object> jndi = new TreeMap<String, Object>();
049:
050: protected void setUp() throws Exception {
051: super .setUp();
052: LoginTestUtil.initialize();
053:
054: jndi.clear();
055: jndi.put("java:comp/path", "fake.jar");
056: jndi.put("java:comp/injections", new InjectionMetaData());
057: }
058:
059: public void testSecureMain() throws Exception {
060: jndi.put("java:comp/callbackHandler",
061: StaticUsernamePasswordCallbackHandler.class.getName());
062:
063: StaticUsernamePasswordCallbackHandler.setUsername("victoria");
064: StaticUsernamePasswordCallbackHandler.setPassword("secret");
065: LoginTestUtil.setAuthGranted();
066:
067: jndi.put("java:comp/mainClass", SecureMain.class.getName());
068: Main.main(new String[0]);
069: }
070:
071: public void testSecureMainFailed() throws Exception {
072: jndi.put("java:comp/callbackHandler",
073: StaticUsernamePasswordCallbackHandler.class.getName());
074:
075: StaticUsernamePasswordCallbackHandler.setUsername("victoria");
076: StaticUsernamePasswordCallbackHandler.setPassword("secret");
077: LoginTestUtil.setAuthDenied();
078:
079: jndi.put("java:comp/mainClass", SecureMain.class.getName());
080: try {
081: Main.main(new String[0]);
082: fail("Expected main method to throw FailedLoginException");
083: } catch (FailedLoginException expected) {
084: }
085: }
086:
087: public static class SecureMain {
088: public static void main(String[] args) {
089: Subject subject = Subject.getSubject(AccessController
090: .getContext());
091:
092: // verify subject
093: assertEquals("Should have one principal", 1, subject
094: .getPrincipals().size());
095: assertEquals("Should have one user principal", 1, subject
096: .getPrincipals(ClientIdentityPrincipal.class)
097: .size());
098: ClientIdentityPrincipal principal = subject.getPrincipals(
099: ClientIdentityPrincipal.class).iterator().next();
100: assertEquals("victoria", principal.getName());
101: assertEquals("SecretIdentity", principal
102: .getClientIdentity());
103:
104: // verify identity
105: assertEquals("SecretIdentity", ClientSecurity.getIdentity());
106: }
107: }
108:
109: public void testNormalMain() throws Exception {
110: jndi.put("java:comp/mainClass", NormalMain.class.getName());
111: Main.main(new String[0]);
112: }
113:
114: public static class NormalMain {
115: public static void main(String[] args) {
116: Subject subject = Subject.getSubject(AccessController
117: .getContext());
118:
119: assertNull("subject is not null", subject);
120:
121: // verify we are not logged in
122: assertNull("ClientSecurity.getIdentity() is not null",
123: ClientSecurity.getIdentity());
124: }
125: }
126:
127: //
128: // Ignore these
129: //
130: public static class MockContextFactoryBuilder implements
131: InitialContextFactoryBuilder {
132: public InitialContextFactory createInitialContextFactory(
133: Hashtable<?, ?> environment) throws NamingException {
134: return new MockContextFactory();
135: }
136: }
137:
138: public static class MockContextFactory implements
139: InitialContextFactory {
140: public Context getInitialContext(Hashtable<?, ?> environment)
141: throws NamingException {
142: return new MockContext();
143: }
144: }
145:
146: public static class MockContext implements Context {
147: public Object lookup(String name) throws NamingException {
148: Object value = jndi.get(name);
149: if (value == null) {
150: throw new NameNotFoundException(name);
151: }
152: return value;
153: }
154:
155: public Object lookup(Name name) throws NamingException {
156: return null;
157: }
158:
159: public void bind(Name name, Object obj) throws NamingException {
160:
161: }
162:
163: public void bind(String name, Object obj)
164: throws NamingException {
165:
166: }
167:
168: public void rebind(Name name, Object obj)
169: throws NamingException {
170:
171: }
172:
173: public void rebind(String name, Object obj)
174: throws NamingException {
175:
176: }
177:
178: public void unbind(Name name) throws NamingException {
179:
180: }
181:
182: public void unbind(String name) throws NamingException {
183:
184: }
185:
186: public void rename(Name oldName, Name newName)
187: throws NamingException {
188:
189: }
190:
191: public void rename(String oldName, String newName)
192: throws NamingException {
193:
194: }
195:
196: public NamingEnumeration<NameClassPair> list(Name name)
197: throws NamingException {
198: return null;
199: }
200:
201: public NamingEnumeration<NameClassPair> list(String name)
202: throws NamingException {
203: return null;
204: }
205:
206: public NamingEnumeration<Binding> listBindings(Name name)
207: throws NamingException {
208: return null;
209: }
210:
211: public NamingEnumeration<Binding> listBindings(String name)
212: throws NamingException {
213: return null;
214: }
215:
216: public void destroySubcontext(Name name) throws NamingException {
217:
218: }
219:
220: public void destroySubcontext(String name)
221: throws NamingException {
222:
223: }
224:
225: public Context createSubcontext(Name name)
226: throws NamingException {
227: return null;
228: }
229:
230: public Context createSubcontext(String name)
231: throws NamingException {
232: return null;
233: }
234:
235: public Object lookupLink(Name name) throws NamingException {
236: return null;
237: }
238:
239: public Object lookupLink(String name) throws NamingException {
240: return null;
241: }
242:
243: public NameParser getNameParser(Name name)
244: throws NamingException {
245: return null;
246: }
247:
248: public NameParser getNameParser(String name)
249: throws NamingException {
250: return null;
251: }
252:
253: public Name composeName(Name name, Name prefix)
254: throws NamingException {
255: return null;
256: }
257:
258: public String composeName(String name, String prefix)
259: throws NamingException {
260: return null;
261: }
262:
263: public Object addToEnvironment(String propName, Object propVal)
264: throws NamingException {
265: return null;
266: }
267:
268: public Object removeFromEnvironment(String propName)
269: throws NamingException {
270: return null;
271: }
272:
273: public Hashtable<?, ?> getEnvironment() throws NamingException {
274: return null;
275: }
276:
277: public void close() throws NamingException {
278:
279: }
280:
281: public String getNameInNamespace() throws NamingException {
282: return null;
283: }
284: }
285: }
|