J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2000-2001 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 4378100 |
| 27 | * @summary LoginContext doesn't reinit modules with new Subject |
| 28 | * if authentication fails |
| 29 | * |
| 30 | * @build ModuleSubject ModuleSubjectModule |
| 31 | * @run main/othervm -Djava.security.auth.login.config=file:${test.src}/ModuleSubject.config ModuleSubject |
| 32 | */ |
| 33 | |
| 34 | import javax.security.auth.*; |
| 35 | import javax.security.auth.login.*; |
| 36 | import java.security.Principal; |
| 37 | |
| 38 | public class ModuleSubject { |
| 39 | |
| 40 | public static void main(String[] args) { |
| 41 | |
| 42 | LoginContext lc = null; |
| 43 | try { |
| 44 | lc = new LoginContext("SampleLogin"); |
| 45 | } catch (LoginException le) { |
| 46 | System.out.println |
| 47 | ("ModuleSubject test failed - login construction failed"); |
| 48 | throw new SecurityException(le.getMessage()); |
| 49 | } |
| 50 | |
| 51 | // first attempt must fail |
| 52 | try { |
| 53 | lc.login(); |
| 54 | throw new SecurityException |
| 55 | ("ModuleSubject test failed: 1st login attempt did not fail!"); |
| 56 | } catch (LoginException le) { |
| 57 | // good! |
| 58 | System.out.println |
| 59 | ("Good: first attempt failed"); |
| 60 | le.printStackTrace(); |
| 61 | } |
| 62 | |
| 63 | if (lc.getSubject() != null) { |
| 64 | throw new SecurityException |
| 65 | ("ModuleSubject test failed - " + |
| 66 | "Subject after failed attempt not null: " + |
| 67 | lc.getSubject().toString()); |
| 68 | } |
| 69 | |
| 70 | // second attempt succeeds, and the correct subject comes back |
| 71 | try { |
| 72 | lc.login(); |
| 73 | java.util.Set principals = lc.getSubject().getPrincipals(); |
| 74 | |
| 75 | if (principals.size() != 1) { |
| 76 | throw new SecurityException("ModuleSubject test failed: " + |
| 77 | "corrupted subject"); |
| 78 | } |
| 79 | java.util.Iterator i = principals.iterator(); |
| 80 | while (i.hasNext()) { |
| 81 | Principal p = (Principal)i.next(); |
| 82 | System.out.println("principal after authentication = " + |
| 83 | p.toString()); |
| 84 | } |
| 85 | } catch (LoginException le) { |
| 86 | System.out.println |
| 87 | ("ModuleSubject test failed - 2nd login attempt failed"); |
| 88 | throw new SecurityException(le.getMessage()); |
| 89 | } |
| 90 | |
| 91 | System.out.println("ModuleSubject test succeeded"); |
| 92 | } |
| 93 | } |