1 7 package org.jboss.test; 8 9 import java.util.Iterator ; 10 import java.util.Set ; 11 import javax.security.auth.login.Configuration ; 12 import javax.security.auth.login.LoginContext ; 13 import javax.security.auth.login.LoginException ; 14 import javax.security.auth.Subject ; 15 16 import junit.framework.TestCase; 17 18 import org.jboss.security.auth.login.XMLLoginConfigImpl; 19 import org.jboss.security.SimplePrincipal; 20 21 public class LoginContextTestCase extends TestCase 22 { 23 24 public LoginContextTestCase(String name) 25 { 26 super(name); 27 } 28 29 protected void setUp() throws Exception 30 { 31 System.setOut(System.err); 32 XMLLoginConfigImpl config = new XMLLoginConfigImpl(); 33 config.setConfigResource("login-config.xml"); 34 config.loadConfig(); 35 Configuration.setConfiguration(config); 36 } 37 38 private void validateSuccessfulLogin(LoginContext lc) throws LoginException 39 { 40 Subject subject = lc.getSubject(); 41 assertTrue("case5 subject != null", subject != null); 42 boolean hasGuest = subject.getPrincipals().contains(new SimplePrincipal("guest")); 43 assertTrue("subject has guest principal", hasGuest); 44 lc.logout(); 45 hasGuest = subject.getPrincipals().contains(new SimplePrincipal("guest")); 46 assertTrue("subject has guest principal", hasGuest == false); 47 Set publicCreds = subject.getPublicCredentials(); 48 assertTrue("public creds has 'A public credential'", 49 publicCreds.contains("A public credential")); 50 Set privateCreds = subject.getPrivateCredentials(); 51 assertTrue("private creds has 'A private credential'", 52 privateCreds.contains("A private credential")); 53 Iterator iter = privateCreds.iterator(); 54 int count = 0; 55 while( iter.hasNext() ) 56 { 57 iter.next(); 58 count ++; 59 } 60 assertTrue("private creds has 1 entry", count == 1); 61 } 62 63 public void testCase1() throws Exception 64 { 65 LoginContext lc = new LoginContext ("case1"); 66 lc.login(); 67 validateSuccessfulLogin(lc); 68 } 69 70 public void testCase2() throws Exception 71 { 72 LoginContext lc = new LoginContext ("case2"); 73 lc.login(); 74 validateSuccessfulLogin(lc); 75 } 76 77 public void testCase3() throws Exception 78 { 79 LoginContext lc = new LoginContext ("case3"); 80 try 81 { 82 lc.login(); 83 fail("LoginContext.login3 did not thrown an exception"); 84 } 85 catch(LoginException e) 86 { 87 e.printStackTrace(); 88 } 89 } 90 91 95 public void testCase4() throws Exception 96 { 97 LoginContext lc = new LoginContext ("case4"); 98 try 99 { 100 lc.login(); 101 fail("LoginContext.login4 did not thrown an exception"); 102 } 103 catch(LoginException e) 104 { 105 e.printStackTrace(); 106 } 107 } 108 109 public void testCase5() throws Exception 110 { 111 LoginContext lc = new LoginContext ("case5"); 112 lc.login(); 113 validateSuccessfulLogin(lc); 114 } 115 public void testCase6() throws Exception 116 { 117 LoginContext lc = new LoginContext ("case6"); 118 lc.login(); 119 validateSuccessfulLogin(lc); 120 } 121 public void testCase7() throws Exception 122 { 123 LoginContext lc = new LoginContext ("case7"); 124 lc.login(); 125 validateSuccessfulLogin(lc); 126 } 127 128 public void testCase8() throws Exception 129 { 130 LoginContext lc = new LoginContext ("case8"); 131 try 132 { 133 lc.login(); 134 fail("LoginContext.login8 did not thrown an exception"); 135 } 136 catch(LoginException e) 137 { 138 e.printStackTrace(); 139 } 140 } 141 142 public void testCase9() throws Exception 143 { 144 LoginContext lc = new LoginContext ("case9"); 145 lc.login(); 146 validateSuccessfulLogin(lc); 147 } 148 149 public void testCase10() throws Exception 150 { 151 LoginContext lc = new LoginContext ("case10"); 152 try 153 { 154 lc.login(); 155 fail("LoginContext.login10 did not thrown an exception"); 156 } 157 catch(LoginException e) 158 { 159 e.printStackTrace(); 160 } 161 } 162 163 public void testCase11() throws Exception 164 { 165 LoginContext lc = new LoginContext ("case11"); 166 lc.login(); 167 validateSuccessfulLogin(lc); 168 } 169 public void testCase12() throws Exception 170 { 171 LoginContext lc = new LoginContext ("case12"); 172 lc.login(); 173 validateSuccessfulLogin(lc); 174 } 175 176 public void testCase13() throws Exception 177 { 178 LoginContext lc = new LoginContext ("case13"); 179 try 180 { 181 lc.login(); 182 fail("LoginContext.login13 did not thrown an exception"); 183 } 184 catch(LoginException e) 185 { 186 e.printStackTrace(); 187 } 188 } 189 190 public void testCase14() throws Exception 191 { 192 LoginContext lc = new LoginContext ("case14"); 193 try 194 { 195 lc.login(); 196 fail("LoginContext.login14 did not thrown an exception"); 197 } 198 catch(LoginException e) 199 { 200 e.printStackTrace(); 201 } 202 } 203 204 public void testCase15() throws Exception 205 { 206 LoginContext lc = new LoginContext ("case15"); 207 try 208 { 209 lc.login(); 210 fail("LoginContext.login15 did not thrown an exception"); 211 } 212 catch(LoginException e) 213 { 214 e.printStackTrace(); 215 } 216 } 217 218 public void testCase16() throws Exception 219 { 220 LoginContext lc = new LoginContext ("case16"); 221 lc.login(); 222 validateSuccessfulLogin(lc); 223 } 224 225 public void testCase17() throws Exception 226 { 227 LoginContext lc = new LoginContext ("case17"); 228 lc.login(); 229 validateSuccessfulLogin(lc); 230 } 231 232 public void testCase18() throws Exception 233 { 234 LoginContext lc = new LoginContext ("case18"); 235 try 236 { 237 lc.login(); 238 fail("LoginContext.login18 did not thrown an exception"); 239 } 240 catch(LoginException e) 241 { 242 e.printStackTrace(); 243 } 244 } 245 246 public void testCase19() throws Exception 247 { 248 LoginContext lc = new LoginContext ("case19"); 249 try 250 { 251 lc.login(); 252 fail("LoginContext.login19 did not thrown an exception"); 253 } 254 catch(LoginException e) 255 { 256 e.printStackTrace(); 257 } 258 } 259 260 public void testCase20() throws Exception 261 { 262 LoginContext lc = new LoginContext ("case20"); 263 try 264 { 265 lc.login(); 266 fail("LoginContext.login20 did not thrown an exception"); 267 } 268 catch(LoginException e) 269 { 270 e.printStackTrace(); 271 } 272 } 273 274 public void testCase21() throws Exception 275 { 276 LoginContext lc = new LoginContext ("case21"); 277 try 278 { 279 lc.login(); 280 } 281 catch(LoginException e) 282 { 283 e.printStackTrace(); 284 } 285 Subject subject = lc.getSubject(); 286 assertTrue("case21 subject == null", subject == null); 287 } 288 289 public void testCase22() throws Exception 290 { 291 LoginContext lc = new LoginContext ("case22"); 292 try 293 { 294 lc.login(); 295 fail("LoginContext.login22 did not thrown an exception"); 296 } 297 catch(LoginException e) 298 { 299 e.printStackTrace(); 300 } 301 } 302 303 } 304 | Popular Tags |