KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > security > AuthenticationSchemeTest


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.security;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertTrue;
24
25 import java.util.Calendar JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28
29 import org.junit.BeforeClass;
30 import org.junit.Ignore;
31 import org.junit.Test;
32
33 import com.sslexplorer.boot.PropertyList;
34 import com.sslexplorer.policyframework.AccessRights;
35 import com.sslexplorer.policyframework.Policy;
36 import com.sslexplorer.policyframework.PolicyConstants;
37 import com.sslexplorer.policyframework.PolicyDatabaseFactory;
38 import com.sslexplorer.policyframework.ResourceType;
39 import com.sslexplorer.realms.Realm;
40 import com.sslexplorer.testcontainer.policyframework.AbstractTestPolicyEnabledResource;
41
42 /**
43  */

44 public class AuthenticationSchemeTest extends AbstractTestPolicyEnabledResource<AuthenticationScheme> {
45     /**
46      * @throws Exception
47      */

48     @BeforeClass
49     public static void oneTimeSetUp() throws Exception JavaDoc {
50         setUp("");
51     }
52     
53     @Override JavaDoc
54     public ResourceType getResourceType() throws Exception JavaDoc {
55       return PolicyConstants.AUTHENTICATION_SCHEMES_RESOURCE_TYPE;
56     }
57
58     @Override JavaDoc
59     public AuthenticationScheme getEmptyResource() throws Exception JavaDoc {
60         Calendar JavaDoc calendar = Calendar.getInstance();
61         return new DefaultAuthenticationScheme(-1, -1, "", "", calendar, calendar, true, 0);
62     }
63
64     @Override JavaDoc
65     public AuthenticationScheme getNormalResource() throws Exception JavaDoc {
66         Calendar JavaDoc calendar = Calendar.getInstance();
67         return new DefaultAuthenticationScheme(getDefaultRealm().getRealmID(), -1, "resourceName", "resourceDescription", calendar, calendar, true, 0);
68     }
69
70     @Override JavaDoc
71     public AuthenticationScheme getNullResource() throws Exception JavaDoc {
72         Calendar JavaDoc calendar = Calendar.getInstance();
73         return new DefaultAuthenticationScheme(-1, -1, null, null, calendar, calendar, true, 0);
74     }
75
76     @Override JavaDoc
77     public AuthenticationScheme createResource(AuthenticationScheme resource) throws Exception JavaDoc {
78         return SystemDatabaseFactory.getInstance().createAuthenticationSchemeSequence(resource.getRealmID(), resource.getResourceName(), resource.getResourceDescription(), resource.getModules(), resource.getEnabled(), resource.getPriorityInt());
79     }
80
81     @Override JavaDoc
82     public AuthenticationScheme updateResource(AuthenticationScheme resource) throws Exception JavaDoc {
83         SystemDatabaseFactory.getInstance().updateAuthenticationSchemeSequence((AuthenticationScheme)resource);
84         return SystemDatabaseFactory.getInstance().getAuthenticationSchemeSequence(resource.getResourceId());
85     }
86     
87     @Override JavaDoc
88     public AuthenticationScheme deleteResource(AuthenticationScheme resource) throws Exception JavaDoc {
89         AuthenticationScheme authenticationScheme = getResource(resource);
90         SystemDatabaseFactory.getInstance().deleteAuthenticationSchemeSequence(resource.getResourceId());
91         return authenticationScheme;
92     }
93
94     @Override JavaDoc
95     public AuthenticationScheme getResource(AuthenticationScheme resource) throws Exception JavaDoc {
96         return SystemDatabaseFactory.getInstance().getAuthenticationSchemeSequence(resource.getResourceId());
97     }
98     
99     @Override JavaDoc
100     public List JavaDoc<AuthenticationScheme> getAllResources() throws Exception JavaDoc {
101         return SystemDatabaseFactory.getInstance().getAuthenticationSchemeSequences();
102     }
103     
104     @Test
105     public void checkResourceAccessRights() throws Exception JavaDoc {
106         Realm realm = getUserService().getRealm(1);
107         Policy policy = createPolicy(realm);
108         User user = createAccount();
109         getPolicyService().grantPolicyToPrincipal(policy, user);
110         
111         AccessRights accessRights = createAssignAccessRights(getResourceType().getPermissionClass());
112         PropertyList selectedPolicies = PropertyList.createFromArray(new int[] {policy.getResourceId()});
113         PolicyDatabaseFactory.getInstance().attachResourceToPolicyList(accessRights, selectedPolicies, getSessionInfo());
114         AuthenticationScheme resource = createResource();
115         assertTrue("Should be, as one already exists.", getPolicyService().isPrincipalGrantedResourcesOfType(user, resource.getResourceType(), null));
116         assertEquals("Should be four.", getPolicyService().getGrantedResourcesOfType(user, getResourceType()).size(), 4);
117         
118         PolicyDatabaseFactory.getInstance().attachResourceToPolicyList(resource, selectedPolicies, getSessionInfo());
119         assertEquals("Should be five.", getPolicyService().getGrantedResourcesOfType(user, getResourceType()).size(), 5);
120         
121         PolicyDatabaseFactory.getInstance().detachResourceFromPolicyList(resource, getSessionInfo());
122         assertEquals("Should be four.", getPolicyService().getGrantedResourcesOfType(user, getResourceType()).size(), 4);
123         getPolicyService().revokePolicyFromPrincipal(policy, user);
124         getUserService().getDefaultUserDatabase().deleteAccount(user);
125         
126         PolicyDatabaseFactory.getInstance().detachResourceFromPolicyList(accessRights, getSessionInfo());
127         deleteResource(resource);
128         getPolicyService().deleteAccessRights(accessRights.getResourceId());
129         getPolicyService().deletePolicy(policy.getResourceId());
130     }
131     
132     /**
133      * We should not be able to create two authentication scheme with the same priority.
134      * However the database doesn't forbidde that at the moment.
135      * @throws Exception
136      */

137     @Test
138     public void createAuthSchWithSamePriority() throws Exception JavaDoc {
139         Calendar JavaDoc calendar = Calendar.getInstance();
140         AuthenticationScheme authenticationScheme = new DefaultAuthenticationScheme(getDefaultRealm().getRealmID(), -1, "resourceName", "resourceDescription", calendar, calendar, true, 1);
141         AuthenticationScheme createdAuthenticationScheme = createResource(authenticationScheme);
142         assertEquals("There should be only one AuthenticationScheme", 1, getAllResources().size());
143         AuthenticationScheme authenticationScheme2 = new DefaultAuthenticationScheme(getDefaultRealm().getRealmID(), -1, "resourceName2", "resourceDescription2", calendar, calendar, true, 1);
144         AuthenticationScheme createdAuthenticationScheme2 = createResource(authenticationScheme2);
145         assertEquals("There should be two AuthenticationScheme", 2, getAllResources().size());
146         deleteResource(createdAuthenticationScheme);
147         deleteResource(createdAuthenticationScheme2);
148         assertTrue("Should have none", getAllResources().isEmpty());
149     }
150     
151     /**
152      * @throws Exception
153      */

154     @Test
155     public void addModulesToAuth() throws Exception JavaDoc {
156         AuthenticationScheme authenticationScheme = (DefaultAuthenticationScheme)createResource();
157         assertEquals("There should be only one AuthenticationScheme", 1, getAllResources().size());
158         
159         for (Iterator JavaDoc ite = AuthenticationModuleManager.getInstance().authenticationModuleDefinitions(); ite.hasNext();) {
160             AuthenticationModuleDefinition definition = (AuthenticationModuleDefinition)ite.next();
161             authenticationScheme.addModule(definition.getName());
162         }
163         updateResource(authenticationScheme);
164         deleteResource(authenticationScheme);
165         assertTrue("Should have none", getAllResources().isEmpty());
166     }
167     
168     /**
169      * @throws Exception
170      */

171     @Test
172     public void deleteModulesFromAuth() throws Exception JavaDoc {
173         AuthenticationScheme authenticationScheme = (DefaultAuthenticationScheme)createResource();
174         assertEquals("There should be only one AuthenticationScheme", 1, getAllResources().size());
175         
176         for (Iterator JavaDoc ite = AuthenticationModuleManager.getInstance().authenticationModuleDefinitions(); ite.hasNext();) {
177             AuthenticationModuleDefinition definition = (AuthenticationModuleDefinition)ite.next();
178             authenticationScheme.removeModule(definition.getName());
179         }
180         updateResource(authenticationScheme);
181         deleteResource(authenticationScheme);
182         assertTrue("Should have none", getAllResources().isEmpty());
183     }
184     
185     /**
186      * @throws Exception
187      */

188     @Test
189     public void registerDeregisterModule() throws Exception JavaDoc {
190         String JavaDoc module = "EmbeddedClientTest";
191         AuthenticationModuleManager.getInstance().registerModule(module, EmbeddedClientAuthenticationModule.class, "security", true, false, true);
192         assertTrue("This module should be registered", AuthenticationModuleManager.getInstance().isRegistered(module));
193         AuthenticationModuleManager.getInstance().deregisterModule(module);
194         assertTrue("This module should not be registered", !AuthenticationModuleManager.getInstance().isRegistered(module));
195     }
196     
197     /**
198      * @throws Exception
199      */

200     @Ignore ("This test should be able to run but there is no control at the moment.")
201     @Test
202     public void deleteAllAuthenticationScheme() throws Exception JavaDoc {
203         List JavaDoc listAuth = SystemDatabaseFactory.getInstance().getAuthenticationSchemeSequences();
204         for (Iterator JavaDoc ite = listAuth.iterator(); ite.hasNext();) {
205             AuthenticationScheme auth = (AuthenticationScheme)ite.next();
206             SystemDatabaseFactory.getInstance().deleteAuthenticationSchemeSequence(auth.getResourceId());
207     }
208        assertEquals("Should have none", 0, SystemDatabaseFactory.getInstance().getAuthenticationSchemeSequences().size());
209     }
210 }
Popular Tags