1 /*
2  * Copyright (c) 2000, 2017, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  * @bug 4378100
27  * @modules jdk.security.auth
28  * @summary LoginContext doesn't reinit modules with new Subject
29  *      if authentication fails
30  *
31  * @build ModuleSubject ModuleSubjectModule
32  * @run main/othervm -Djava.security.auth.login.config=file:${test.src}/ModuleSubject.config ModuleSubject
33  */
34 
35 import java.security.Principal;
36 import javax.security.auth.login.LoginContext;
37 import javax.security.auth.login.LoginException;
38 
39 public class ModuleSubject {
40 
main(String[] args)41     public static void main(String[] args) {
42 
43         LoginContext lc = null;
44         try {
45             lc = new LoginContext("SampleLogin");
46         } catch (LoginException le) {
47             System.out.println
48                 ("ModuleSubject test failed - login construction failed");
49             throw new SecurityException(le.getMessage());
50         }
51 
52         // first attempt must fail
53         try {
54             lc.login();
55             throw new SecurityException
56                 ("ModuleSubject test failed: 1st login attempt did not fail!");
57         } catch (LoginException le) {
58             // good!
59             System.out.println
60                 ("Good: first attempt failed");
61             le.printStackTrace();
62         }
63 
64         if (lc.getSubject() != null) {
65             throw new SecurityException
66                 ("ModuleSubject test failed - " +
67                 "Subject after failed attempt not null: " +
68                 lc.getSubject().toString());
69         }
70 
71         // second attempt succeeds, and the correct subject comes back
72         try {
73             lc.login();
74             java.util.Set principals = lc.getSubject().getPrincipals();
75 
76             if (principals.size() != 1) {
77                 throw new SecurityException("ModuleSubject test failed: " +
78                                         "corrupted subject");
79             }
80             java.util.Iterator i = principals.iterator();
81             while (i.hasNext()) {
82                 Principal p = (Principal)i.next();
83                 System.out.println("principal after authentication = " +
84                                 p.toString());
85             }
86         } catch (LoginException le) {
87             System.out.println
88                 ("ModuleSubject test failed - 2nd login attempt failed");
89             throw new SecurityException(le.getMessage());
90         }
91 
92         System.out.println("ModuleSubject test succeeded");
93     }
94 }
95