Path: blob/master/test/jdk/javax/security/auth/login/Configuration/GetInstance.java
51695 views
/*1* Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 626831526* @bug 627381227* @modules jdk.security.auth28* @summary Configuration should be provider-based29* @build GetInstanceConfigSpi GetInstanceProvider30* @run main/othervm -Djava.security.auth.login.config==${test.src}${/}GetInstance.config GetInstance31*/3233import java.security.NoSuchAlgorithmException;34import java.security.NoSuchProviderException;35import java.security.Provider;36import java.security.Security;37import java.security.URIParameter;38import java.io.File;39import java.net.URI;40import javax.security.auth.login.AppConfigurationEntry;41import javax.security.auth.login.Configuration;4243public class GetInstance {4445private static final String JAVA_CONFIG = "JavaLoginConfig";4647private static final String MOD0 = "com.foo.Module";48private static final String MOD1 = "com.bar.Module";49private static final String MOD2 = "com.foobar.Module";50private static final String MOD3 = "Module";5152private static class BadParam implements Configuration.Parameters { }5354public static void main(String[] args) throws Exception {5556int testnum = 1;57GetInstance gi = new GetInstance();5859testnum = gi.testDefault(testnum);60testnum = gi.testStringProvider(testnum);61testnum = gi.testProvider(testnum);62testnum = gi.testCustomImpl(testnum);63testnum = gi.testURIParam(testnum);64testnum = gi.testException(testnum);65}6667private int testDefault(int testnum) throws Exception {68// get an instance of the default ConfigSpiFile69Configuration c = Configuration.getInstance(JAVA_CONFIG, null);70doTest(c, testnum++);7172// get an instance of FooConfig73try {74c = Configuration.getInstance("FooConfig", null);75throw new SecurityException("test " + testnum++ + " failed");76} catch (NoSuchAlgorithmException nsae) {77// good78System.out.println("test " + testnum++ + " passed");79}8081return testnum;82}8384private int testStringProvider(int testnum) throws Exception {85// get an instance of JavaLoginConfig from SUN86Configuration c = Configuration.getInstance(JAVA_CONFIG, null, "SUN");87doTest(c, testnum++);8889// get an instance of JavaLoginConfig from SunRsaSign90try {91c = Configuration.getInstance(JAVA_CONFIG, null, "SunRsaSign");92throw new SecurityException("test " + testnum++ + " failed");93} catch (NoSuchAlgorithmException nsae) {94// good95System.out.println("test " + testnum++ + " passed");96}9798// get an instance of JavaLoginConfig from FOO99try {100c = Configuration.getInstance(JAVA_CONFIG, null, "FOO");101throw new SecurityException("test " + testnum++ + " failed");102} catch (NoSuchProviderException nspe) {103// good104System.out.println("test " + testnum++ + " passed");105}106107return testnum;108}109110private int testProvider(int testnum) throws Exception {111// get an instance of JavaLoginConfig from SUN112Configuration c = Configuration.getInstance(JAVA_CONFIG,113null,114Security.getProvider("SUN"));115doTest(c, testnum++);116117// get an instance of JavaLoginConfig from SunRsaSign118try {119c = Configuration.getInstance(JAVA_CONFIG,120null,121Security.getProvider("SunRsaSign"));122throw new SecurityException("test " + testnum++ + " failed");123} catch (NoSuchAlgorithmException nsae) {124// good125System.out.println("test " + testnum++ + " passed");126}127128return testnum;129}130131private int testCustomImpl(int testnum) throws Exception {132Provider customProvider = new GetInstanceProvider();133Configuration c = Configuration.getInstance("GetInstanceConfigSpi",134null,135customProvider);136doCustomTest(c, testnum++, customProvider);137return testnum;138}139140private int testURIParam(int testnum) throws Exception {141// get an instance of JavaLoginConfig142// from SUN and have it read from the URI143144File file = new File(System.getProperty("test.src", "."),145"GetInstance.configURI");146URI uri = file.toURI();147URIParameter uriParam = new URIParameter(uri);148Configuration c = Configuration.getInstance(JAVA_CONFIG, uriParam);149doTestURI(c, uriParam, testnum++);150151return testnum;152}153154private int testException(int testnum) throws Exception {155// get an instance of JavaLoginConfig156// from SUN and have it read from the bad URI157158File file = new File(System.getProperty("test.src", "."),159"GetInstance.bad.configURI");160URI uri = file.toURI();161URIParameter uriParam = new URIParameter(uri);162163try {164Configuration c = Configuration.getInstance(JAVA_CONFIG, uriParam);165throw new SecurityException("expected IOException");166} catch (NoSuchAlgorithmException nsae) {167if (nsae.getCause() instanceof java.io.IOException) {168System.out.println("exception test passed: " +169nsae.getCause().getMessage());170} else {171throw new SecurityException("expected IOException");172}173}174175// pass bad param176try {177Configuration c = Configuration.getInstance(JAVA_CONFIG,178new BadParam());179throw new SecurityException("test " + testnum++ + " failed");180} catch (IllegalArgumentException iae) {181// good182System.out.println("test " + testnum++ + " passed");183}184185try {186Configuration c = Configuration.getInstance(JAVA_CONFIG,187new BadParam(),188"SUN");189throw new SecurityException("test " + testnum++ + " failed");190} catch (IllegalArgumentException iae) {191// good192System.out.println("test " + testnum++ + " passed");193}194195try {196Configuration c = Configuration.getInstance(JAVA_CONFIG,197new BadParam(),198Security.getProvider("SUN"));199throw new SecurityException("test " + testnum++ + " failed");200} catch (IllegalArgumentException iae) {201// good202System.out.println("test " + testnum++ + " passed");203}204205return testnum;206}207208private int doCommon(Configuration c, int testnum) throws Exception {209210AppConfigurationEntry[] entries = c.getAppConfigurationEntry("EMPTY");211if (entries == null) {212System.out.println("test " + testnum + ".1 passed");213} else {214throw new SecurityException("test " + testnum + ".1 failed");215}216217entries = c.getAppConfigurationEntry("one");218if (entries.length == 1 &&219MOD0.equals(entries[0].getLoginModuleName()) &&220AppConfigurationEntry.LoginModuleControlFlag.REQUIRED ==221entries[0].getControlFlag()) {222System.out.println("test " + testnum + ".2 passed");223} else {224throw new SecurityException("test " + testnum + ".2 failed");225}226227entries = c.getAppConfigurationEntry("two");228if (entries.length == 2 &&229MOD0.equals(entries[0].getLoginModuleName()) &&230AppConfigurationEntry.LoginModuleControlFlag.SUFFICIENT ==231entries[0].getControlFlag() &&232MOD1.equals(entries[1].getLoginModuleName()) &&233AppConfigurationEntry.LoginModuleControlFlag.REQUIRED ==234entries[1].getControlFlag()) {235System.out.println("test " + testnum + ".3 passed");236} else {237throw new SecurityException("test " + testnum + ".3 failed");238}239240entries = c.getAppConfigurationEntry("three");241if (entries.length == 3 &&242MOD0.equals(entries[0].getLoginModuleName()) &&243AppConfigurationEntry.LoginModuleControlFlag.SUFFICIENT ==244entries[0].getControlFlag() &&245MOD1.equals(entries[1].getLoginModuleName()) &&246AppConfigurationEntry.LoginModuleControlFlag.REQUIRED ==247entries[1].getControlFlag() &&248MOD2.equals(entries[2].getLoginModuleName()) &&249AppConfigurationEntry.LoginModuleControlFlag.OPTIONAL ==250entries[2].getControlFlag()) {251System.out.println("test " + testnum + ".4 passed");252} else {253throw new SecurityException("test " + testnum + ".4 failed");254}255256return testnum;257}258259private void doCustomTest(Configuration c,260int testnum,261Provider custom) throws Exception {262263testnum = doCommon(c, testnum);264265// test getProvider266if (custom == c.getProvider() &&267"GetInstanceProvider".equals(c.getProvider().getName())) {268System.out.println("test " + testnum + " (getProvider) passed");269} else {270throw new SecurityException271("test " + testnum + " (getProvider) failed");272}273274// test getType275if ("GetInstanceConfigSpi".equals(c.getType())) {276System.out.println("test " + testnum + "(getType) passed");277} else {278throw new SecurityException("test " + testnum +279" (getType) failed");280}281}282283private void doTest(Configuration c, int testnum) throws Exception {284testnum = doCommon(c, testnum);285286// test getProvider287if ("SUN".equals(c.getProvider().getName())) {288System.out.println("test " + testnum + " (getProvider) passed");289} else {290throw new SecurityException("test " + testnum +291" (getProvider) failed");292}293294// test getType295if (JAVA_CONFIG.equals(c.getType())) {296System.out.println("test " + testnum + " (getType) passed");297} else {298throw new SecurityException("test " + testnum +299" (getType) failed");300}301}302303private void doTestURI(Configuration c,304Configuration.Parameters uriParam,305int testnum) throws Exception {306307AppConfigurationEntry[] entries = c.getAppConfigurationEntry("four");308if (entries.length == 4 &&309MOD0.equals(entries[0].getLoginModuleName()) &&310AppConfigurationEntry.LoginModuleControlFlag.SUFFICIENT ==311entries[0].getControlFlag() &&312MOD1.equals(entries[1].getLoginModuleName()) &&313AppConfigurationEntry.LoginModuleControlFlag.REQUIRED ==314entries[1].getControlFlag() &&315MOD2.equals(entries[2].getLoginModuleName()) &&316AppConfigurationEntry.LoginModuleControlFlag.OPTIONAL ==317entries[2].getControlFlag() &&318MOD3.equals(entries[3].getLoginModuleName()) &&319AppConfigurationEntry.LoginModuleControlFlag.REQUIRED ==320entries[3].getControlFlag()) {321System.out.println("test " + testnum + ".1 passed");322} else {323throw new SecurityException("test " + testnum + ".1 failed");324}325326// test getProvider327if ("SUN".equals(c.getProvider().getName())) {328System.out.println("test " + testnum + " (getProvider) passed");329} else {330throw new SecurityException("test " + testnum +331" (getProvider) failed");332}333334// test getType335if (JAVA_CONFIG.equals(c.getType())) {336System.out.println("test " + testnum + " (getType) passed");337} else {338throw new SecurityException("test " + testnum +339" (getType) failed");340}341342// test getParameters343if (uriParam.equals(c.getParameters())) {344System.out.println("test " + testnum + " (getParameters) passed");345} else {346throw new SecurityException("test " + testnum +347" (getParameters) failed");348}349}350}351352353