Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/security/auth/login/LoginContext/DynamicConfigurationTest.java
38860 views
1
/*
2
* Copyright (c) 2004, 2015, 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
import java.io.IOException;
25
import javax.security.auth.Subject;
26
import javax.security.auth.callback.Callback;
27
import javax.security.auth.callback.CallbackHandler;
28
import javax.security.auth.callback.NameCallback;
29
import javax.security.auth.callback.PasswordCallback;
30
import javax.security.auth.callback.UnsupportedCallbackException;
31
import javax.security.auth.login.Configuration;
32
import javax.security.auth.login.LoginContext;
33
import javax.security.auth.login.LoginException;
34
35
/**
36
* @test
37
* @bug 8050427 4703361
38
* @summary Test case for RFE: 4703361. Tests the Dynamic Configuration of
39
* Authentication Modules with different methods
40
* @compile SmartLoginModule.java DummyLoginModule.java MyConfiguration.java
41
* @run main/othervm DynamicConfigurationTest
42
*/
43
public class DynamicConfigurationTest {
44
45
public static void main(String... args) {
46
String rightConfigName = "PT";
47
String wrongConfigName = "NT";
48
char[] rightPwd = new char[]{'t', 'e', 's', 't', 'P', 'a', 's', 's',
49
'w', 'o', 'r', 'd', '1'};
50
char[] wrongPwd = new char[]{'w', 'r', 'o', 'n', 'g', 'P', 'a', 's',
51
's','w', 'o', 'r', 'd'};
52
53
// Test with wrong configuration name
54
// Expect LoginException when initiate a new LoginContext object
55
testConfigName(wrongConfigName, true);
56
System.out.println("Wrong Config Name Test passed ");
57
58
// Spedify two loginModules: SmartLoginModule and DummyLoginModule
59
// Flags: required-required
60
// Test with right password for SmartLoginModule
61
// No exception is expected
62
Configuration cf = new MyConfiguration();
63
testLogin(rightConfigName, rightPwd, cf, false);
64
System.out.println("Positive test passed");
65
66
// Spedify two loginModules: SmartLoginModule and DummyLoginModule
67
// Flags: required-required
68
// Test with wrong password for SmartLoginModule
69
// Expect LoginException by calling LoginContext.login() method
70
testLogin(rightConfigName, wrongPwd, cf, true);
71
System.out.println("Should fail test passed");
72
73
// Spedify two loginModules: SmartLoginModule and DummyLoginModule
74
// Change the flags from required-required to optional-sufficient
75
// Test with wrong password for SmartLoginModule, while DummyLoginModule
76
// always passes
77
// No Exception is expected
78
cf = new MyConfiguration(true);
79
testLogin(rightConfigName, wrongPwd, cf, false);
80
System.out.println("One module fails where are other module succeeeds "
81
+ "Test passed with optional-sufficient flags");
82
}
83
84
public static void testConfigName(String confName,
85
boolean expectException) {
86
String expectedMsg = "No LoginModules configured for " + confName;
87
try {
88
LoginContext lc = new LoginContext(confName, new Subject(),
89
new MyCallbackHandler(), new MyConfiguration());
90
91
if (expectException) {
92
throw new RuntimeException("Wrong Config Name Test failed: "
93
+ "expected LoginException not thrown.");
94
}
95
} catch (LoginException le) {
96
if (!expectException || !le.getMessage().equals(expectedMsg)) {
97
System.out.println("Wrong Config Name Test failed: "
98
+ "received Unexpected exception.");
99
throw new RuntimeException(le);
100
}
101
}
102
}
103
104
public static void testLogin(String confName, char[] passwd,
105
Configuration cf, boolean expectException) {
106
try {
107
CallbackHandler ch = new MyCallbackHandler("testUser", passwd);
108
LoginContext lc = new LoginContext(confName, new Subject(),
109
ch, cf);
110
lc.login();
111
if (expectException) {
112
throw new RuntimeException("Login Test failed: "
113
+ "expected LoginException not thrown");
114
}
115
} catch (LoginException le) {
116
if (!expectException) {
117
System.out.println("Login Test failed: "
118
+ "received Unexpected exception.");
119
throw new RuntimeException(le);
120
}
121
}
122
}
123
}
124
125
/**
126
* The application simulates the CallbackHandler. It simulates! which means all
127
* process to get username and password is ignored. We have to take this
128
* approach for automation purpose. So, this is not a real world example at all.
129
*/
130
class MyCallbackHandler implements CallbackHandler {
131
132
String userName;
133
char[] password;
134
135
/**
136
* This is simply a workaround approach for IO approach to get username and
137
* password. For automation purpose only.
138
*/
139
public MyCallbackHandler() {
140
super();
141
}
142
143
public MyCallbackHandler(String username, char[] password) {
144
super();
145
userName = username;
146
this.password = password;
147
}
148
149
@Override
150
public void handle(Callback[] callbacks) throws IOException,
151
UnsupportedCallbackException {
152
for (Callback callback : callbacks) {
153
if (callback instanceof NameCallback) {
154
NameCallback nc = (NameCallback) callback;
155
nc.setName(userName);
156
} else if (callback instanceof PasswordCallback) {
157
PasswordCallback pc = (PasswordCallback) callback;
158
pc.setPassword(password);
159
} else {
160
throw new UnsupportedCallbackException(callback,
161
"Unrecognized Callback");
162
}
163
}
164
}
165
}
166
167