1 18 19 package org.apache.activemq.jaas; 20 21 import junit.framework.TestCase; 22 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.security.Principal ; 26 import java.security.cert.X509Certificate ; 27 import java.util.HashMap ; 28 import java.util.HashSet ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Set ; 32 import java.util.Vector ; 33 34 import javax.security.auth.Subject ; 35 import javax.security.auth.login.LoginException ; 36 37 public class CertificateLoginModuleTest extends TestCase { 38 private final String userName = "testUser"; 39 private final List groupNames = new Vector (); 40 private StubCertificateLoginModule loginModule; 41 42 private Subject subject; 43 44 public CertificateLoginModuleTest() { 45 groupNames.add("testGroup1"); 46 groupNames.add("testGroup2"); 47 groupNames.add("testGroup3"); 48 groupNames.add("testGroup4"); 49 } 50 51 protected void setUp() throws Exception { 52 subject = new Subject (); 53 } 54 55 protected void tearDown() throws Exception { 56 } 57 58 private void loginWithCredentials(String userName, Set groupNames) throws LoginException { 59 loginModule = new StubCertificateLoginModule(userName, new HashSet (groupNames)); 60 JaasCertificateCallbackHandler callbackHandler = new JaasCertificateCallbackHandler(null); 61 62 loginModule.initialize(subject, callbackHandler, null, new HashMap ()); 63 64 loginModule.login(); 65 loginModule.commit(); 66 } 67 68 private void checkPrincipalsMatch(Subject subject) { 69 boolean nameFound = false; 70 boolean groupsFound[] = new boolean[groupNames.size()]; 71 for (int i = 0; i < groupsFound.length; ++i) { 72 groupsFound[i] = false; 73 } 74 75 for (Iterator iter = subject.getPrincipals().iterator(); iter.hasNext(); ) { 76 Principal currentPrincipal = (Principal ) iter.next(); 77 78 if (currentPrincipal instanceof UserPrincipal) { 79 if (((UserPrincipal)currentPrincipal).getName().equals(userName)) { 80 if (nameFound == false) { 81 nameFound = true; 82 } else { 83 fail("UserPrincipal found twice."); 84 } 85 86 } else { 87 fail("Unknown UserPrincipal found."); 88 } 89 90 } else if (currentPrincipal instanceof GroupPrincipal) { 91 int principalIdx = groupNames.indexOf(((GroupPrincipal)currentPrincipal).getName()); 92 93 if (principalIdx < 0) { 94 fail("Unknown GroupPrincipal found."); 95 } 96 97 if (groupsFound[principalIdx] == false) { 98 groupsFound[principalIdx] = true; 99 } else { 100 fail("GroupPrincipal found twice."); 101 } 102 } else { 103 fail("Unknown Principal type found."); 104 } 105 } 106 } 107 108 public void testLoginSuccess() throws IOException { 109 try { 110 loginWithCredentials(userName, new HashSet (groupNames)); 111 } catch (Exception e) { 112 fail("Unable to login: " + e.getMessage()); 113 } 114 115 checkPrincipalsMatch(subject); 116 } 117 118 public void testLoginFailure() throws IOException { 119 boolean loginFailed = false; 120 121 try { 122 loginWithCredentials(null, new HashSet ()); 123 } catch (LoginException e) { 124 loginFailed = true; 125 } 126 127 if (!loginFailed) { 128 fail("Logged in with unknown certificate."); 129 } 130 } 131 132 public void testLogOut() throws IOException { 133 try { 134 loginWithCredentials(userName, new HashSet (groupNames)); 135 } catch (Exception e) { 136 fail("Unable to login: " + e.getMessage()); 137 } 138 139 loginModule.logout(); 140 141 assertEquals("logout should have cleared Subject principals.", 0, subject.getPrincipals().size()); 142 } 143 } 144 145 | Popular Tags |