KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > security > PropertiesLoginModuleTest


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.jbi.security;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.net.URL JavaDoc;
22
23 import javax.security.auth.Subject JavaDoc;
24 import javax.security.auth.callback.Callback JavaDoc;
25 import javax.security.auth.callback.CallbackHandler JavaDoc;
26 import javax.security.auth.callback.NameCallback JavaDoc;
27 import javax.security.auth.callback.PasswordCallback JavaDoc;
28 import javax.security.auth.callback.UnsupportedCallbackException JavaDoc;
29 import javax.security.auth.login.FailedLoginException JavaDoc;
30 import javax.security.auth.login.LoginContext JavaDoc;
31 import javax.security.auth.login.LoginException JavaDoc;
32
33 import junit.framework.TestCase;
34
35
36 /**
37  *
38  */

39 public class PropertiesLoginModuleTest extends TestCase {
40     
41     static {
42         String JavaDoc path = System.getProperty("java.security.auth.login.config");
43         if (path == null) {
44             URL JavaDoc resource = PropertiesLoginModuleTest.class.getResource("login.properties");
45             if (resource != null) {
46                 path = new File JavaDoc(resource.getFile()).getAbsolutePath();
47                 System.setProperty("java.security.auth.login.config", path);
48             }
49         }
50         System.out.println("Path to login config: " + path);
51     }
52
53     public void testLogin() throws LoginException JavaDoc {
54         LoginContext JavaDoc context = new LoginContext JavaDoc("servicemix-domain", new CallbackHandler JavaDoc() {
55             public void handle(Callback JavaDoc[] callbacks) throws IOException JavaDoc, UnsupportedCallbackException JavaDoc {
56                 for (int i = 0; i < callbacks.length; i++) {
57                     if (callbacks[i] instanceof NameCallback JavaDoc) {
58                         ((NameCallback JavaDoc) callbacks[i]).setName("first");
59                     } else if (callbacks[i] instanceof PasswordCallback JavaDoc) {
60                         ((PasswordCallback JavaDoc) callbacks[i]).setPassword("secret".toCharArray());
61                     } else {
62                         throw new UnsupportedCallbackException JavaDoc(callbacks[i]);
63                     }
64                 }
65             }
66         });
67         context.login();
68
69         Subject JavaDoc subject = context.getSubject();
70
71         assertEquals("Should have three principals", 3, subject.getPrincipals().size());
72         assertEquals("Should have one user principal", 1, subject.getPrincipals(UserPrincipal.class).size());
73         assertEquals("Should have two group principals", 2, subject.getPrincipals(GroupPrincipal.class).size());
74
75         context.logout();
76
77         assertEquals("Should have zero principals", 0, subject.getPrincipals().size());
78     }
79
80     public void testBadUseridLogin() throws Exception JavaDoc {
81         LoginContext JavaDoc context = new LoginContext JavaDoc("servicemix-domain", new CallbackHandler JavaDoc() {
82             public void handle(Callback JavaDoc[] callbacks) throws IOException JavaDoc, UnsupportedCallbackException JavaDoc {
83                 for (int i = 0; i < callbacks.length; i++) {
84                     if (callbacks[i] instanceof NameCallback JavaDoc) {
85                         ((NameCallback JavaDoc) callbacks[i]).setName("BAD");
86                     } else if (callbacks[i] instanceof PasswordCallback JavaDoc) {
87                         ((PasswordCallback JavaDoc) callbacks[i]).setPassword("secret".toCharArray());
88                     } else {
89                         throw new UnsupportedCallbackException JavaDoc(callbacks[i]);
90                     }
91                 }
92             }
93         });
94         try {
95             context.login();
96             fail("Should have thrown a FailedLoginException");
97         } catch (FailedLoginException JavaDoc doNothing) {
98         }
99
100     }
101
102     public void testBadPWLogin() throws Exception JavaDoc {
103         LoginContext JavaDoc context = new LoginContext JavaDoc("servicemix-domain", new CallbackHandler JavaDoc() {
104             public void handle(Callback JavaDoc[] callbacks) throws IOException JavaDoc, UnsupportedCallbackException JavaDoc {
105                 for (int i = 0; i < callbacks.length; i++) {
106                     if (callbacks[i] instanceof NameCallback JavaDoc) {
107                         ((NameCallback JavaDoc) callbacks[i]).setName("first");
108                     } else if (callbacks[i] instanceof PasswordCallback JavaDoc) {
109                         ((PasswordCallback JavaDoc) callbacks[i]).setPassword("BAD".toCharArray());
110                     } else {
111                         throw new UnsupportedCallbackException JavaDoc(callbacks[i]);
112                     }
113                 }
114             }
115         });
116         try {
117             context.login();
118             fail("Should have thrown a FailedLoginException");
119         } catch (FailedLoginException JavaDoc doNothing) {
120         }
121
122     }
123 }
124
Popular Tags