1 8 9 package test.mx4j.tools.remote; 10 11 import java.io.BufferedInputStream ; 12 import java.io.BufferedWriter ; 13 import java.io.ByteArrayInputStream ; 14 import java.io.InputStream ; 15 import java.io.StringWriter ; 16 import java.security.Principal ; 17 import java.util.Set ; 18 import javax.security.auth.Subject ; 19 20 import mx4j.tools.remote.PasswordAuthenticator; 21 import test.MX4JTestCase; 22 23 26 public class PasswordAuthenticatorTest extends MX4JTestCase 27 { 28 public PasswordAuthenticatorTest(String s) 29 { 30 super(s); 31 } 32 33 public void testAuthenticationWithNullCredentials() throws Exception  34 { 35 PasswordAuthenticator authenticator = new PasswordAuthenticator(preparePasswords(new String [0])); 36 Object credentials = null; 37 try 38 { 39 authenticator.authenticate(credentials); 40 fail(); 41 } 42 catch (SecurityException x) 43 { 44 } 45 } 46 47 public void testAuthenticationWithBadCredentials() throws Exception  48 { 49 PasswordAuthenticator authenticator = new PasswordAuthenticator(preparePasswords(new String [0])); 50 Object credentials = new Object (); 51 try 52 { 53 authenticator.authenticate(credentials); 54 fail(); 55 } 56 catch (SecurityException x) 57 { 58 } 59 } 60 61 public void testAuthenticationWithCredentialsNull() throws Exception  62 { 63 PasswordAuthenticator authenticator = new PasswordAuthenticator(preparePasswords(new String [0])); 64 Object credentials = new String [2]; 65 try 66 { 67 authenticator.authenticate(credentials); 68 fail(); 69 } 70 catch (SecurityException x) 71 { 72 } 73 } 74 75 public void testAuthenticationWithUnknwonCredentials() throws Exception  76 { 77 PasswordAuthenticator authenticator = new PasswordAuthenticator(preparePasswords(new String []{"user1", "password1"})); 78 Object credentials = new String []{"dummy", null}; 79 try 80 { 81 authenticator.authenticate(credentials); 82 fail(); 83 } 84 catch (SecurityException x) 85 { 86 } 87 } 88 89 public void testAuthenticationWithWrongCredentials() throws Exception  90 { 91 String user = "user1"; 92 PasswordAuthenticator authenticator = new PasswordAuthenticator(preparePasswords(new String []{user, "password1"})); 93 Object credentials = new String []{user, null}; 94 try 95 { 96 authenticator.authenticate(credentials); 97 fail(); 98 } 99 catch (SecurityException x) 100 { 101 } 102 } 103 104 public void testAuthenticationPlainSentClear() throws Exception  105 { 106 String user = "user1"; 107 String password = "password1"; 108 PasswordAuthenticator authenticator = new PasswordAuthenticator(preparePasswords(new String []{user, password})); 109 Object credentials = new String []{user, password}; 110 Subject subject = authenticator.authenticate(credentials); 112 assertNotNull(subject); 113 Set principals = subject.getPrincipals(); 114 assertEquals(principals.size(), 1); 115 Principal principal = (Principal )principals.iterator().next(); 116 assertEquals(principal.getName(), user); 117 } 118 119 public void testAuthenticationPlainSentObfuscated() throws Exception  120 { 121 String user = "user1"; 122 String password = "password1"; 123 PasswordAuthenticator authenticator = new PasswordAuthenticator(preparePasswords(new String []{user, password})); 124 Object credentials = new String []{user, PasswordAuthenticator.obfuscatePassword(password)}; 125 Subject subject = authenticator.authenticate(credentials); 127 assertNotNull(subject); 128 Set principals = subject.getPrincipals(); 129 assertEquals(principals.size(), 1); 130 Principal principal = (Principal )principals.iterator().next(); 131 assertEquals(principal.getName(), user); 132 } 133 134 public void testAuthenticationObfuscatedSentClear() throws Exception  135 { 136 String user = "user1"; 137 String password = "password1"; 138 PasswordAuthenticator authenticator = new PasswordAuthenticator(preparePasswords(new String []{user, PasswordAuthenticator.obfuscatePassword(password)})); 139 Object credentials = new String []{user, password}; 140 Subject subject = authenticator.authenticate(credentials); 142 assertNotNull(subject); 143 Set principals = subject.getPrincipals(); 144 assertEquals(principals.size(), 1); 145 Principal principal = (Principal )principals.iterator().next(); 146 assertEquals(principal.getName(), user); 147 } 148 149 public void testAuthenticationObfuscatedSentObfuscated() throws Exception  150 { 151 String user = "user1"; 152 String password = "password1"; 153 PasswordAuthenticator authenticator = new PasswordAuthenticator(preparePasswords(new String []{user, PasswordAuthenticator.obfuscatePassword(password)})); 154 Object credentials = new String []{user, PasswordAuthenticator.obfuscatePassword(password)}; 155 Subject subject = authenticator.authenticate(credentials); 157 assertNotNull(subject); 158 Set principals = subject.getPrincipals(); 159 assertEquals(principals.size(), 1); 160 Principal principal = (Principal )principals.iterator().next(); 161 assertEquals(principal.getName(), user); 162 } 163 164 private InputStream preparePasswords(String [] pairs) throws Exception  165 { 166 StringWriter sw = new StringWriter (); 167 BufferedWriter bw = new BufferedWriter (sw); 168 for (int i = 0; i < pairs.length; i += 2) 169 { 170 String user = pairs[i]; 171 String password = pairs[i + 1]; 172 bw.write(user); 173 bw.write(':'); 174 bw.write(password); 175 bw.newLine(); 176 } 177 bw.close(); 178 179 return new BufferedInputStream (new ByteArrayInputStream (sw.toString().getBytes())); 180 } 181 } 182 | Popular Tags |