Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/security/auth/login/Configuration/GetInstance.java
38861 views
/*1* Copyright (c) 2005, 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* @summary Configuration should be provider-based28* @build GetInstanceConfigSpi GetInstanceProvider29* @run main/othervm -Djava.security.auth.login.config==${test.src}${/}GetInstance.config GetInstance30*/3132import javax.security.auth.login.*;3334import java.security.*;35import java.io.File;36import java.net.URI;3738public class GetInstance {3940private static final String JAVA_CONFIG = "JavaLoginConfig";4142private static final String MOD0 = "com.foo.Module";43private static final String MOD1 = "com.bar.Module";44private static final String MOD2 = "com.foobar.Module";45private static final String MOD3 = "Module";4647private static class BadParam implements Configuration.Parameters { }4849public static void main(String[] args) throws Exception {5051int testnum = 1;52GetInstance gi = new GetInstance();5354testnum = gi.testDefault(testnum);55testnum = gi.testStringProvider(testnum);56testnum = gi.testProvider(testnum);57testnum = gi.testCustomImpl(testnum);58testnum = gi.testURIParam(testnum);59testnum = gi.testException(testnum);60}6162private int testDefault(int testnum) throws Exception {63// get an instance of the default ConfigSpiFile64Configuration c = Configuration.getInstance(JAVA_CONFIG, null);65doTest(c, testnum++);6667// get an instance of FooConfig68try {69c = Configuration.getInstance("FooConfig", null);70throw new SecurityException("test " + testnum++ + " failed");71} catch (NoSuchAlgorithmException nsae) {72// good73System.out.println("test " + testnum++ + " passed");74}7576return testnum;77}7879private int testStringProvider(int testnum) throws Exception {80// get an instance of JavaLoginConfig from SUN81Configuration c = Configuration.getInstance(JAVA_CONFIG, null, "SUN");82doTest(c, testnum++);8384// get an instance of JavaLoginConfig from SunRsaSign85try {86c = Configuration.getInstance(JAVA_CONFIG, null, "SunRsaSign");87throw new SecurityException("test " + testnum++ + " failed");88} catch (NoSuchAlgorithmException nsae) {89// good90System.out.println("test " + testnum++ + " passed");91}9293// get an instance of JavaLoginConfig from FOO94try {95c = Configuration.getInstance(JAVA_CONFIG, null, "FOO");96throw new SecurityException("test " + testnum++ + " failed");97} catch (NoSuchProviderException nspe) {98// good99System.out.println("test " + testnum++ + " passed");100}101102return testnum;103}104105private int testProvider(int testnum) throws Exception {106// get an instance of JavaLoginConfig from SUN107Configuration c = Configuration.getInstance(JAVA_CONFIG,108null,109Security.getProvider("SUN"));110doTest(c, testnum++);111112// get an instance of JavaLoginConfig from SunRsaSign113try {114c = Configuration.getInstance(JAVA_CONFIG,115null,116Security.getProvider("SunRsaSign"));117throw new SecurityException("test " + testnum++ + " failed");118} catch (NoSuchAlgorithmException nsae) {119// good120System.out.println("test " + testnum++ + " passed");121}122123return testnum;124}125126private int testCustomImpl(int testnum) throws Exception {127Provider customProvider = new GetInstanceProvider();128Configuration c = Configuration.getInstance("GetInstanceConfigSpi",129null,130customProvider);131doCustomTest(c, testnum++, customProvider);132return testnum;133}134135private int testURIParam(int testnum) throws Exception {136// get an instance of JavaLoginConfig137// from SUN and have it read from the URI138139File file = new File(System.getProperty("test.src", "."),140"GetInstance.configURI");141URI uri = file.toURI();142URIParameter uriParam = new URIParameter(uri);143Configuration c = Configuration.getInstance(JAVA_CONFIG, uriParam);144doTestURI(c, uriParam, testnum++);145146return testnum;147}148149private int testException(int testnum) throws Exception {150// get an instance of JavaLoginConfig151// from SUN and have it read from the bad URI152153File file = new File(System.getProperty("test.src", "."),154"GetInstance.bad.configURI");155URI uri = file.toURI();156URIParameter uriParam = new URIParameter(uri);157158try {159Configuration c = Configuration.getInstance(JAVA_CONFIG, uriParam);160throw new SecurityException("expected IOException");161} catch (NoSuchAlgorithmException nsae) {162if (nsae.getCause() instanceof java.io.IOException) {163System.out.println("exception test passed: " +164nsae.getCause().getMessage());165} else {166throw new SecurityException("expected IOException");167}168}169170// pass bad param171try {172Configuration c = Configuration.getInstance(JAVA_CONFIG,173new BadParam());174throw new SecurityException("test " + testnum++ + " failed");175} catch (IllegalArgumentException iae) {176// good177System.out.println("test " + testnum++ + " passed");178}179180try {181Configuration c = Configuration.getInstance(JAVA_CONFIG,182new BadParam(),183"SUN");184throw new SecurityException("test " + testnum++ + " failed");185} catch (IllegalArgumentException iae) {186// good187System.out.println("test " + testnum++ + " passed");188}189190try {191Configuration c = Configuration.getInstance(JAVA_CONFIG,192new BadParam(),193Security.getProvider("SUN"));194throw new SecurityException("test " + testnum++ + " failed");195} catch (IllegalArgumentException iae) {196// good197System.out.println("test " + testnum++ + " passed");198}199200return testnum;201}202203private int doCommon(Configuration c, int testnum) throws Exception {204205AppConfigurationEntry[] entries = c.getAppConfigurationEntry("EMPTY");206if (entries == null) {207System.out.println("test " + testnum + ".1 passed");208} else {209throw new SecurityException("test " + testnum + ".1 failed");210}211212entries = c.getAppConfigurationEntry("one");213if (entries.length == 1 &&214MOD0.equals(entries[0].getLoginModuleName()) &&215AppConfigurationEntry.LoginModuleControlFlag.REQUIRED ==216entries[0].getControlFlag()) {217System.out.println("test " + testnum + ".2 passed");218} else {219throw new SecurityException("test " + testnum + ".2 failed");220}221222entries = c.getAppConfigurationEntry("two");223if (entries.length == 2 &&224MOD0.equals(entries[0].getLoginModuleName()) &&225AppConfigurationEntry.LoginModuleControlFlag.SUFFICIENT ==226entries[0].getControlFlag() &&227MOD1.equals(entries[1].getLoginModuleName()) &&228AppConfigurationEntry.LoginModuleControlFlag.REQUIRED ==229entries[1].getControlFlag()) {230System.out.println("test " + testnum + ".3 passed");231} else {232throw new SecurityException("test " + testnum + ".3 failed");233}234235entries = c.getAppConfigurationEntry("three");236if (entries.length == 3 &&237MOD0.equals(entries[0].getLoginModuleName()) &&238AppConfigurationEntry.LoginModuleControlFlag.SUFFICIENT ==239entries[0].getControlFlag() &&240MOD1.equals(entries[1].getLoginModuleName()) &&241AppConfigurationEntry.LoginModuleControlFlag.REQUIRED ==242entries[1].getControlFlag() &&243MOD2.equals(entries[2].getLoginModuleName()) &&244AppConfigurationEntry.LoginModuleControlFlag.OPTIONAL ==245entries[2].getControlFlag()) {246System.out.println("test " + testnum + ".4 passed");247} else {248throw new SecurityException("test " + testnum + ".4 failed");249}250251return testnum;252}253254private void doCustomTest(Configuration c,255int testnum,256Provider custom) throws Exception {257258testnum = doCommon(c, testnum);259260// test getProvider261if (custom == c.getProvider() &&262"GetInstanceProvider".equals(c.getProvider().getName())) {263System.out.println("test " + testnum + " (getProvider) passed");264} else {265throw new SecurityException266("test " + testnum + " (getProvider) failed");267}268269// test getType270if ("GetInstanceConfigSpi".equals(c.getType())) {271System.out.println("test " + testnum + "(getType) passed");272} else {273throw new SecurityException("test " + testnum +274" (getType) failed");275}276}277278private void doTest(Configuration c, int testnum) throws Exception {279testnum = doCommon(c, testnum);280281// test getProvider282if ("SUN".equals(c.getProvider().getName())) {283System.out.println("test " + testnum + " (getProvider) passed");284} else {285throw new SecurityException("test " + testnum +286" (getProvider) failed");287}288289// test getType290if (JAVA_CONFIG.equals(c.getType())) {291System.out.println("test " + testnum + " (getType) passed");292} else {293throw new SecurityException("test " + testnum +294" (getType) failed");295}296}297298private void doTestURI(Configuration c,299Configuration.Parameters uriParam,300int testnum) throws Exception {301302AppConfigurationEntry[] entries = c.getAppConfigurationEntry("four");303if (entries.length == 4 &&304MOD0.equals(entries[0].getLoginModuleName()) &&305AppConfigurationEntry.LoginModuleControlFlag.SUFFICIENT ==306entries[0].getControlFlag() &&307MOD1.equals(entries[1].getLoginModuleName()) &&308AppConfigurationEntry.LoginModuleControlFlag.REQUIRED ==309entries[1].getControlFlag() &&310MOD2.equals(entries[2].getLoginModuleName()) &&311AppConfigurationEntry.LoginModuleControlFlag.OPTIONAL ==312entries[2].getControlFlag() &&313MOD3.equals(entries[3].getLoginModuleName()) &&314AppConfigurationEntry.LoginModuleControlFlag.REQUIRED ==315entries[3].getControlFlag()) {316System.out.println("test " + testnum + ".1 passed");317} else {318throw new SecurityException("test " + testnum + ".1 failed");319}320321// test getProvider322if ("SUN".equals(c.getProvider().getName())) {323System.out.println("test " + testnum + " (getProvider) passed");324} else {325throw new SecurityException("test " + testnum +326" (getProvider) failed");327}328329// test getType330if (JAVA_CONFIG.equals(c.getType())) {331System.out.println("test " + testnum + " (getType) passed");332} else {333throw new SecurityException("test " + testnum +334" (getType) failed");335}336337// test getParameters338if (uriParam.equals(c.getParameters())) {339System.out.println("test " + testnum + " (getParameters) passed");340} else {341throw new SecurityException("test " + testnum +342" (getParameters) failed");343}344}345}346347348