KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jguard > ext > authentication > manager > XmlAuthenticationManagerTest


1 /*
2 jGuard is a security framework based on top of jaas (java authentication and authorization security).
3 it is written for web applications, to resolve simply, access control problems.
4 version $Name$
5 http://sourceforge.net/projects/jguard/
6
7 Copyright (C) 2004 Charles GAY
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
23
24 jGuard project home page:
25 http://sourceforge.net/projects/jguard/
26
27 */

28 package net.sf.jguard.ext.authentication.manager;
29
30 import java.net.URL JavaDoc;
31 import java.security.Principal JavaDoc;
32 import java.util.Collection JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.HashSet JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.Map JavaDoc;
37 import java.util.Set JavaDoc;
38
39 import javax.security.auth.Subject JavaDoc;
40
41 import junit.framework.Assert;
42 import junit.framework.TestCase;
43 import junitx.util.PrivateAccessor;
44 import net.sf.jguard.core.CoreConstants;
45 import net.sf.jguard.core.authentication.credentials.JGuardCredential;
46 import net.sf.jguard.core.principals.RolePrincipal;
47 import net.sf.jguard.ext.SecurityConstants;
48 import net.sf.jguard.ext.authentication.AuthenticationException;
49 import net.sf.jguard.ext.principals.PrincipalUtils;
50 import net.sf.jguard.ext.registration.SubjectTemplate;
51
52 public class XmlAuthenticationManagerTest extends TestCase {
53
54     private AuthenticationManager auth;
55
56
57     protected void setUp() throws Exception JavaDoc {
58         super.setUp();
59         PrivateAccessor.setField(AuthenticationManagerFactory.class,"authenticationManager",null);
60         auth = getXmlAuthenticationManager();
61         
62     }
63
64
65     protected void tearDown() throws Exception JavaDoc {
66         super.tearDown();
67     }
68
69     public void initAuthenticationManager() {
70         Map JavaDoc settings = new HashMap JavaDoc();
71         URL JavaDoc url = getClass().getResource("/jGuardUsersPrincipals.xml");
72         settings.put(SecurityConstants.AUTHENTICATION_XML_FILE_LOCATION, url.toString());
73         try {
74             AuthenticationManagerFactory.createAuthenticationManager(XmlAuthenticationManager.class.getName(), settings);
75         } catch (AuthenticationException e1) {
76             TestCase.fail(" authenticationDAoFactory not properly initialized ");
77         }
78     }
79
80     public void testAddAndRemoveRolePrincipal(){
81         initAuthenticationManager();
82         AuthenticationManager authManager = AuthenticationManagerFactory.getAuthenticationManager();
83         
84     }
85
86     public void testCreateUser(){
87
88         SubjectTemplate st = buildSubjectTemplate();
89         Subject JavaDoc user = null;
90         try {
91             user = auth.createUser(st);
92             System.out.println(" test succeed ");
93
94             System.out.println(user);
95
96             //test find user
97
Collection JavaDoc credentials = user.getPublicCredentials();
98             credentials.addAll(user.getPrivateCredentials());
99             Collection JavaDoc users = auth.findUsers(credentials);
100             assertTrue("multiple users are found user unicity is not here",users.size()==1);
101             Iterator JavaDoc itUsers = users.iterator();
102             Subject JavaDoc user2 = (Subject JavaDoc)itUsers.next();
103             assertTrue(user.equals(user2));
104
105
106         } catch (AuthenticationException e) {
107             TestCase.fail(e.getMessage());
108         }
109
110     }
111
112
113     private SubjectTemplate buildSubjectTemplate() {
114         SubjectTemplate st = new SubjectTemplate();
115
116         //we build a regular subjectTemplate
117
JGuardCredential jcred1 = new JGuardCredential();
118         jcred1.setId("login");
119         jcred1.setValue("toto"+System.currentTimeMillis());
120         JGuardCredential jcred2 = new JGuardCredential();
121         jcred2.setId("password");
122         jcred2.setValue("toto"+System.currentTimeMillis());
123         Set JavaDoc privRequiredCred = new HashSet JavaDoc();
124         privRequiredCred.add(jcred1);
125         privRequiredCred.add(jcred2);
126         st.setPrivateRequiredCredentials(privRequiredCred);
127         return st;
128     }
129     
130     
131     private Principal JavaDoc buildRolePrincipal(){
132         Principal JavaDoc ppal = PrincipalUtils.getPrincipal("net.sf.jguard.core.principals.RolePrincipal",RolePrincipal.getName("stuff"));
133         
134         return ppal;
135     }
136
137
138     public void testCreateUserWithEmptySubjectTemplate(){
139
140         //we build an empty Subject Template
141
SubjectTemplate st = new SubjectTemplate();
142         st.setPrivateOptionalCredentials(new HashSet JavaDoc());
143         st.setPublicOptionalCredentials(new HashSet JavaDoc());
144         st.setPublicRequiredCredentials(new HashSet JavaDoc());
145         st.setPrivateRequiredCredentials(new HashSet JavaDoc());
146         st.setPrincipals(new HashSet JavaDoc());
147         try {
148             auth.createUser(st);
149         } catch (AuthenticationException e) {
150             System.out.println(" test succeeed => an exception is the normal result login and password private credentials are required "+e.getMessage());
151         }
152     }
153
154
155     private AuthenticationManager getXmlAuthenticationManager() {
156
157         Map JavaDoc options = new HashMap JavaDoc();
158         options.put(CoreConstants.APPLICATION_NAME,"jGuardExample");
159         URL JavaDoc url = getClass().getResource("/jGuardUsersPrincipals.xml");
160
161         options.put(SecurityConstants.AUTHENTICATION_XML_FILE_LOCATION,url.toString());
162         try {
163             AuthenticationManagerFactory.createAuthenticationManager(XmlAuthenticationManager.class.getName(),options);
164         } catch (AuthenticationException e1) {
165             e1.printStackTrace();
166             TestCase.fail(" authenticationManager fail!! ");
167         }
168         AuthenticationManager auth = AuthenticationManagerFactory.getAuthenticationManager();
169         return auth;
170     }
171
172
173     public void testRemoveUser(){
174
175         Set JavaDoc users = null;
176         try {
177             users = auth.getUsers();
178         } catch (AuthenticationException e) {
179             TestCase.fail(" auth.getUsers()!! "+e.getMessage());
180         }
181         System.out.println(users);
182
183         //we build a regular subjectTemplate
184
SubjectTemplate st = new SubjectTemplate();
185
186         JGuardCredential jcred1 = new JGuardCredential();
187         jcred1.setId("login");
188         jcred1.setValue("abc");
189         JGuardCredential jcred2 = new JGuardCredential();
190         jcred2.setId("password");
191         jcred2.setValue("toto"+System.currentTimeMillis());
192         Set JavaDoc privRequiredCred = new HashSet JavaDoc();
193         privRequiredCred.add(jcred1);
194         privRequiredCred.add(jcred2);
195         st.setPrivateRequiredCredentials(privRequiredCred);
196
197         try {
198             auth.createUser(st);
199         } catch (AuthenticationException e) {
200             TestCase.fail(" user creation fail!! "+e.getMessage());
201         }
202         Subject JavaDoc sj = new Subject JavaDoc();
203         JGuardCredential cred = new JGuardCredential();
204         cred.setId("login");
205         cred.setValue("abc");
206         sj.getPublicCredentials().add(cred);
207         try {
208             auth.deleteUser(sj);
209         } catch (AuthenticationException e) {
210             TestCase.fail(" remove user fail!! "+e.getMessage());
211         }
212
213         Set JavaDoc coll = new HashSet JavaDoc();
214         coll.add(cred);
215         try {
216             Collection JavaDoc usersFound = auth.findUsers(coll);
217             System.out.println(usersFound);
218         } catch (AuthenticationException e) {
219             TestCase.fail(" remove user fail!! "+e.getMessage());
220         }
221     }
222     
223     public void testCreateAndRemoveUSer(){
224         
225         SubjectTemplate st = buildSubjectTemplate();
226         Subject JavaDoc user = null;
227         try {
228             user = auth.createUser(st);
229         } catch (AuthenticationException e) {
230             TestCase.fail(e.getMessage());
231         }
232         Assert.assertNotNull(user);
233         try {
234             auth.deleteUser(user);
235             Set JavaDoc users = auth.getUsers();
236             assertTrue(!users.contains(user));
237         } catch (AuthenticationException e) {
238             TestCase.fail(e.getMessage());
239         }
240     }
241     
242     
243     public void deletePrincipal(){
244         
245         Principal JavaDoc ppal = buildRolePrincipal();
246         try {
247             boolean result= auth.deletePrincipal(ppal);
248         } catch (AuthenticationException e) {
249             TestCase.fail(e.getMessage());
250         }
251     }
252     
253     
254
255
256     public void testUpdateUser(){
257         Subject JavaDoc user = createUser(auth);
258         System.out.println(user);
259         JGuardCredential identityCred = new JGuardCredential();
260         identityCred.setId("login");
261         identityCred.setValue("toto");
262         try {
263             auth.updateUser(identityCred,user);
264         } catch (AuthenticationException e) {
265             TestCase.fail(" update user fail!! "+e.getMessage());
266         }
267     }
268
269
270     private Subject JavaDoc createUser(AuthenticationManager auth) {
271
272         SubjectTemplate st = buildSubjectTemplate();
273         Subject JavaDoc user = null;
274         try {
275             user = auth.createUser(st);
276
277         } catch (AuthenticationException e) {
278             TestCase.fail(e.getMessage());
279         }
280         return user;
281     }
282
283
284     public void testGetSubjectTemplate(){
285         SubjectTemplate st = null;
286         try {
287              st = auth.getSubjectTemplate("default");
288         } catch (AuthenticationException e) {
289             TestCase.fail("SubjectTemplate intitiled default cannot be grabbed from the XML file "+e.getMessage());
290         }
291         System.out.println("subjectTemplate="+st);
292     }
293
294 }
295
Popular Tags