1/* 2 * Copyright (c) 2005, 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 6268315 27 * @bug 6273812 28 * @modules jdk.security.auth 29 * @summary Configuration should be provider-based 30 * @build GetInstanceConfigSpi GetInstanceProvider 31 * @run main/othervm -Djava.security.auth.login.config==${test.src}${/}GetInstance.config GetInstance 32 */ 33 34import java.security.NoSuchAlgorithmException; 35import java.security.NoSuchProviderException; 36import java.security.Provider; 37import java.security.Security; 38import java.security.URIParameter; 39import java.io.File; 40import java.net.URI; 41import javax.security.auth.login.AppConfigurationEntry; 42import javax.security.auth.login.Configuration; 43 44public class GetInstance { 45 46 private static final String JAVA_CONFIG = "JavaLoginConfig"; 47 48 private static final String MOD0 = "com.foo.Module"; 49 private static final String MOD1 = "com.bar.Module"; 50 private static final String MOD2 = "com.foobar.Module"; 51 private static final String MOD3 = "Module"; 52 53 private static class BadParam implements Configuration.Parameters { } 54 55 public static void main(String[] args) throws Exception { 56 57 int testnum = 1; 58 GetInstance gi = new GetInstance(); 59 60 testnum = gi.testDefault(testnum); 61 testnum = gi.testStringProvider(testnum); 62 testnum = gi.testProvider(testnum); 63 testnum = gi.testCustomImpl(testnum); 64 testnum = gi.testURIParam(testnum); 65 testnum = gi.testException(testnum); 66 } 67 68 private int testDefault(int testnum) throws Exception { 69 // get an instance of the default ConfigSpiFile 70 Configuration c = Configuration.getInstance(JAVA_CONFIG, null); 71 doTest(c, testnum++); 72 73 // get an instance of FooConfig 74 try { 75 c = Configuration.getInstance("FooConfig", null); 76 throw new SecurityException("test " + testnum++ + " failed"); 77 } catch (NoSuchAlgorithmException nsae) { 78 // good 79 System.out.println("test " + testnum++ + " passed"); 80 } 81 82 return testnum; 83 } 84 85 private int testStringProvider(int testnum) throws Exception { 86 // get an instance of JavaLoginConfig from SUN 87 Configuration c = Configuration.getInstance(JAVA_CONFIG, null, "SUN"); 88 doTest(c, testnum++); 89 90 // get an instance of JavaLoginConfig from SunRsaSign 91 try { 92 c = Configuration.getInstance(JAVA_CONFIG, null, "SunRsaSign"); 93 throw new SecurityException("test " + testnum++ + " failed"); 94 } catch (NoSuchAlgorithmException nsae) { 95 // good 96 System.out.println("test " + testnum++ + " passed"); 97 } 98 99 // get an instance of JavaLoginConfig from FOO 100 try { 101 c = Configuration.getInstance(JAVA_CONFIG, null, "FOO"); 102 throw new SecurityException("test " + testnum++ + " failed"); 103 } catch (NoSuchProviderException nspe) { 104 // good 105 System.out.println("test " + testnum++ + " passed"); 106 } 107 108 return testnum; 109 } 110 111 private int testProvider(int testnum) throws Exception { 112 // get an instance of JavaLoginConfig from SUN 113 Configuration c = Configuration.getInstance(JAVA_CONFIG, 114 null, 115 Security.getProvider("SUN")); 116 doTest(c, testnum++); 117 118 // get an instance of JavaLoginConfig from SunRsaSign 119 try { 120 c = Configuration.getInstance(JAVA_CONFIG, 121 null, 122 Security.getProvider("SunRsaSign")); 123 throw new SecurityException("test " + testnum++ + " failed"); 124 } catch (NoSuchAlgorithmException nsae) { 125 // good 126 System.out.println("test " + testnum++ + " passed"); 127 } 128 129 return testnum; 130 } 131 132 private int testCustomImpl(int testnum) throws Exception { 133 Provider customProvider = new GetInstanceProvider(); 134 Configuration c = Configuration.getInstance("GetInstanceConfigSpi", 135 null, 136 customProvider); 137 doCustomTest(c, testnum++, customProvider); 138 return testnum; 139 } 140 141 private int testURIParam(int testnum) throws Exception { 142 // get an instance of JavaLoginConfig 143 // from SUN and have it read from the URI 144 145 File file = new File(System.getProperty("test.src", "."), 146 "GetInstance.configURI"); 147 URI uri = file.toURI(); 148 URIParameter uriParam = new URIParameter(uri); 149 Configuration c = Configuration.getInstance(JAVA_CONFIG, uriParam); 150 doTestURI(c, uriParam, testnum++); 151 152 return testnum; 153 } 154 155 private int testException(int testnum) throws Exception { 156 // get an instance of JavaLoginConfig 157 // from SUN and have it read from the bad URI 158 159 File file = new File(System.getProperty("test.src", "."), 160 "GetInstance.bad.configURI"); 161 URI uri = file.toURI(); 162 URIParameter uriParam = new URIParameter(uri); 163 164 try { 165 Configuration c = Configuration.getInstance(JAVA_CONFIG, uriParam); 166 throw new SecurityException("expected IOException"); 167 } catch (NoSuchAlgorithmException nsae) { 168 if (nsae.getCause() instanceof java.io.IOException) { 169 System.out.println("exception test passed: " + 170 nsae.getCause().getMessage()); 171 } else { 172 throw new SecurityException("expected IOException"); 173 } 174 } 175 176 // pass bad param 177 try { 178 Configuration c = Configuration.getInstance(JAVA_CONFIG, 179 new BadParam()); 180 throw new SecurityException("test " + testnum++ + " failed"); 181 } catch (IllegalArgumentException iae) { 182 // good 183 System.out.println("test " + testnum++ + " passed"); 184 } 185 186 try { 187 Configuration c = Configuration.getInstance(JAVA_CONFIG, 188 new BadParam(), 189 "SUN"); 190 throw new SecurityException("test " + testnum++ + " failed"); 191 } catch (IllegalArgumentException iae) { 192 // good 193 System.out.println("test " + testnum++ + " passed"); 194 } 195 196 try { 197 Configuration c = Configuration.getInstance(JAVA_CONFIG, 198 new BadParam(), 199 Security.getProvider("SUN")); 200 throw new SecurityException("test " + testnum++ + " failed"); 201 } catch (IllegalArgumentException iae) { 202 // good 203 System.out.println("test " + testnum++ + " passed"); 204 } 205 206 return testnum; 207 } 208 209 private int doCommon(Configuration c, int testnum) throws Exception { 210 211 AppConfigurationEntry[] entries = c.getAppConfigurationEntry("EMPTY"); 212 if (entries == null) { 213 System.out.println("test " + testnum + ".1 passed"); 214 } else { 215 throw new SecurityException("test " + testnum + ".1 failed"); 216 } 217 218 entries = c.getAppConfigurationEntry("one"); 219 if (entries.length == 1 && 220 MOD0.equals(entries[0].getLoginModuleName()) && 221 AppConfigurationEntry.LoginModuleControlFlag.REQUIRED == 222 entries[0].getControlFlag()) { 223 System.out.println("test " + testnum + ".2 passed"); 224 } else { 225 throw new SecurityException("test " + testnum + ".2 failed"); 226 } 227 228 entries = c.getAppConfigurationEntry("two"); 229 if (entries.length == 2 && 230 MOD0.equals(entries[0].getLoginModuleName()) && 231 AppConfigurationEntry.LoginModuleControlFlag.SUFFICIENT == 232 entries[0].getControlFlag() && 233 MOD1.equals(entries[1].getLoginModuleName()) && 234 AppConfigurationEntry.LoginModuleControlFlag.REQUIRED == 235 entries[1].getControlFlag()) { 236 System.out.println("test " + testnum + ".3 passed"); 237 } else { 238 throw new SecurityException("test " + testnum + ".3 failed"); 239 } 240 241 entries = c.getAppConfigurationEntry("three"); 242 if (entries.length == 3 && 243 MOD0.equals(entries[0].getLoginModuleName()) && 244 AppConfigurationEntry.LoginModuleControlFlag.SUFFICIENT == 245 entries[0].getControlFlag() && 246 MOD1.equals(entries[1].getLoginModuleName()) && 247 AppConfigurationEntry.LoginModuleControlFlag.REQUIRED == 248 entries[1].getControlFlag() && 249 MOD2.equals(entries[2].getLoginModuleName()) && 250 AppConfigurationEntry.LoginModuleControlFlag.OPTIONAL == 251 entries[2].getControlFlag()) { 252 System.out.println("test " + testnum + ".4 passed"); 253 } else { 254 throw new SecurityException("test " + testnum + ".4 failed"); 255 } 256 257 return testnum; 258 } 259 260 private void doCustomTest(Configuration c, 261 int testnum, 262 Provider custom) throws Exception { 263 264 testnum = doCommon(c, testnum); 265 266 // test getProvider 267 if (custom == c.getProvider() && 268 "GetInstanceProvider".equals(c.getProvider().getName())) { 269 System.out.println("test " + testnum + " (getProvider) passed"); 270 } else { 271 throw new SecurityException 272 ("test " + testnum + " (getProvider) failed"); 273 } 274 275 // test getType 276 if ("GetInstanceConfigSpi".equals(c.getType())) { 277 System.out.println("test " + testnum + "(getType) passed"); 278 } else { 279 throw new SecurityException("test " + testnum + 280 " (getType) failed"); 281 } 282 } 283 284 private void doTest(Configuration c, int testnum) throws Exception { 285 testnum = doCommon(c, testnum); 286 287 // test getProvider 288 if ("SUN".equals(c.getProvider().getName())) { 289 System.out.println("test " + testnum + " (getProvider) passed"); 290 } else { 291 throw new SecurityException("test " + testnum + 292 " (getProvider) failed"); 293 } 294 295 // test getType 296 if (JAVA_CONFIG.equals(c.getType())) { 297 System.out.println("test " + testnum + " (getType) passed"); 298 } else { 299 throw new SecurityException("test " + testnum + 300 " (getType) failed"); 301 } 302 } 303 304 private void doTestURI(Configuration c, 305 Configuration.Parameters uriParam, 306 int testnum) throws Exception { 307 308 AppConfigurationEntry[] entries = c.getAppConfigurationEntry("four"); 309 if (entries.length == 4 && 310 MOD0.equals(entries[0].getLoginModuleName()) && 311 AppConfigurationEntry.LoginModuleControlFlag.SUFFICIENT == 312 entries[0].getControlFlag() && 313 MOD1.equals(entries[1].getLoginModuleName()) && 314 AppConfigurationEntry.LoginModuleControlFlag.REQUIRED == 315 entries[1].getControlFlag() && 316 MOD2.equals(entries[2].getLoginModuleName()) && 317 AppConfigurationEntry.LoginModuleControlFlag.OPTIONAL == 318 entries[2].getControlFlag() && 319 MOD3.equals(entries[3].getLoginModuleName()) && 320 AppConfigurationEntry.LoginModuleControlFlag.REQUIRED == 321 entries[3].getControlFlag()) { 322 System.out.println("test " + testnum + ".1 passed"); 323 } else { 324 throw new SecurityException("test " + testnum + ".1 failed"); 325 } 326 327 // test getProvider 328 if ("SUN".equals(c.getProvider().getName())) { 329 System.out.println("test " + testnum + " (getProvider) passed"); 330 } else { 331 throw new SecurityException("test " + testnum + 332 " (getProvider) failed"); 333 } 334 335 // test getType 336 if (JAVA_CONFIG.equals(c.getType())) { 337 System.out.println("test " + testnum + " (getType) passed"); 338 } else { 339 throw new SecurityException("test " + testnum + 340 " (getType) failed"); 341 } 342 343 // test getParameters 344 if (uriParam.equals(c.getParameters())) { 345 System.out.println("test " + testnum + " (getParameters) passed"); 346 } else { 347 throw new SecurityException("test " + testnum + 348 " (getParameters) failed"); 349 } 350 } 351} 352