ResetConfigModule.java revision 6073:cea72c2bf071
1/*
2 * Copyright (c) 2002, 2011, 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 4633622
27 * @summary  bug in LoginContext when Configuration is subclassed
28 * @build ResetConfigModule ResetModule
29 * @run main ResetConfigModule
30 */
31
32import javax.security.auth.*;
33import javax.security.auth.login.*;
34import javax.security.auth.spi.*;
35import javax.security.auth.callback.*;
36import java.util.*;
37
38public class ResetConfigModule {
39
40    public static void main(String[] args) throws Exception {
41
42        Configuration previousConf = Configuration.getConfiguration();
43        ClassLoader previousCL = Thread.currentThread().getContextClassLoader();
44
45        try {
46            Thread.currentThread().setContextClassLoader(
47                    ResetConfigModule.class.getClassLoader());
48            Configuration.setConfiguration(new MyConfig());
49
50            LoginContext lc = new LoginContext("test");
51            try {
52                lc.login();
53                throw new SecurityException("test 1 failed");
54            } catch (LoginException le) {
55                if (le.getCause() != null &&
56                    le.getCause() instanceof SecurityException) {
57                    System.out.println("good so far");
58                } else {
59                    throw le;
60                }
61            }
62
63            LoginContext lc2 = new LoginContext("test2");
64            try {
65                lc2.login();
66                throw new SecurityException("test 2 failed");
67            } catch (LoginException le) {
68                if (le.getCause() != null &&
69                    le.getCause()  instanceof SecurityException) {
70                    System.out.println("test succeeded");
71                } else {
72                    throw le;
73                }
74            }
75        } finally {
76            Configuration.setConfiguration(previousConf);
77            Thread.currentThread().setContextClassLoader(previousCL);
78        }
79    }
80}
81
82class MyConfig extends Configuration {
83    private AppConfigurationEntry[] entries = {
84        new AppConfigurationEntry("ResetModule",
85                AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
86                new HashMap()) };
87    public AppConfigurationEntry[] getAppConfigurationEntry(String name) {
88        return entries;
89    }
90    public void refresh() { }
91}
92