1 /*
2  * Copyright (c) 2000, 2001, 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 4377181
27  * @summary Provide default configurable CallbackHandlers
28  *
29  * @build DefaultHandler DefaultHandlerImpl DefaultHandlerModule
30  * @run main/othervm -Djava.security.auth.login.config=file:${test.src}/DefaultHandler.config DefaultHandler
31  */
32 
33 import javax.security.auth.*;
34 import javax.security.auth.login.*;
35 
36 public class DefaultHandler {
37 
main(String[] args)38     public static void main(String[] args) {
39 
40         // first test if a default is not provided.
41         // we should get an exception
42 
43         LoginContext lc = null;
44         try {
45             lc = new LoginContext("SampleLogin");
46         } catch (LoginException le) {
47             System.out.println
48                 ("DefaultHandler test failed - login construction failed");
49             throw new SecurityException(le.getMessage());
50         }
51 
52         try {
53             lc.login();
54             throw new SecurityException
55                 ("DefaultHandler test failed: got a handler!");
56         } catch (LoginException le) {
57             // good!
58             System.out.println
59                 ("Good: CallbackHandler implementation not found");
60             le.printStackTrace();
61         }
62 
63         // set the security property for the default handler
64         java.security.Security.setProperty("auth.login.defaultCallbackHandler",
65                 "DefaultHandlerImpl");
66 
67         // now test to see if the default handler is picked up.
68         // this should succeed.
69 
70         LoginContext lc2 = null;
71         try {
72             lc2 = new LoginContext("SampleLogin");
73         } catch (LoginException le) {
74             System.out.println
75                 ("DefaultHandler test failed - constructing LoginContext");
76             throw new SecurityException(le.getMessage());
77         }
78 
79         try {
80             lc2.login();
81         } catch (LoginException le) {
82             System.out.println
83                 ("DefaultHandler test failed - login method");
84             throw new SecurityException(le.getMessage());
85         }
86 
87         System.out.println("DefaultHandler test succeeded");
88     }
89 }
90