Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/provider/PolicyParser/TokenStore.java
38853 views
/*1* Copyright (c) 2003, 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 491914726* @summary Support for token-based KeyStores27*/2829import java.io.*;30import java.util.*;31import sun.security.provider.*;3233public class TokenStore {3435private static final String POLICY_NO_STORE =36"grant { permission java.security.AllPermission; };";3738private static final String POLICY_URL =39"keystore \"file:${test.src}${/}TokenStore.keystore\";" +40"grant signedby \"POLICY_URL\" {" +41" permission java.security.AllPermission;" +42"};" ;4344private static final String POLICY_URL_T =45"keystore \"file:${test.src}${/}TokenStore.keystore\", \"JKS\";"+46"grant signedby \"POLICY_URL_T\" {" +47" permission java.security.AllPermission;" +48"};" ;4950private static final String POLICY_URL_T_P =51"keystore \"file:${test.src}${/}TokenStore.keystore\"," +52" \"JKS\", \"SUN\";" +53"grant signedby \"POLICY_URL_T_P\" {" +54" permission java.security.AllPermission;" +55"};" ;5657private static final String POLICY_URL_PWD =58"keystore \"file:${test.src}${/}TokenStore.keystore\";" +59"keystorePasswordURL \"file:${test.src}${/}TokenStore.pwd\";" +60"grant signedby \"POLICY_URL\" {" +61" permission java.security.AllPermission;" +62"};" ;6364private static final String POLICY_URL_T_P_PWD =65"keystore \"file:${test.src}${/}TokenStore.keystore\"," +66" \"JKS\", \"SUN\";" +67"keystorePasswordURL \"file:${test.src}${/}TokenStore.pwd\";" +68"grant signedby \"POLICY_URL_T_P\" {" +69" permission java.security.AllPermission;" +70"};" ;7172private static final String POLICY_PASS_NO_STORE =73"keystorePasswordURL \"file:${test.src}${/}TokenStore.pwd\";" +74"grant signedby \"POLICY_URL_T_P\" {" +75" permission java.security.AllPermission;" +76"};" ;7778public static void main(String[] args) throws Exception {7980// test no key store in policy8182PolicyParser p = new PolicyParser();83p.read(new StringReader(POLICY_NO_STORE));84doNoStore(p);85StringWriter sw = new StringWriter();86p.write(sw);87PolicyParser newP = new PolicyParser();88newP.read(new StringReader(sw.toString()));89doNoStore(p);9091// test policy keystore + URL9293p = new PolicyParser();94p.read(new StringReader(POLICY_URL));95doURL(p, true);96sw = new StringWriter();97p.write(sw);98newP = new PolicyParser();99newP.read(new StringReader(sw.toString()));100doURL(p, true);101102// test policy keystore + URL + type103104p = new PolicyParser();105p.read(new StringReader(POLICY_URL_T));106doURL_T(p, true);107sw = new StringWriter();108p.write(sw);109newP = new PolicyParser();110newP.read(new StringReader(sw.toString()));111doURL_T(p, true);112113// test policy keystore + URL + type + provider114115p = new PolicyParser();116p.read(new StringReader(POLICY_URL_T_P));117doURL_T_P(p, true);118sw = new StringWriter();119p.write(sw);120newP = new PolicyParser();121newP.read(new StringReader(sw.toString()));122doURL_T_P(p, true);123124// test policy keystore + URL + password125126p = new PolicyParser();127p.read(new StringReader(POLICY_URL_PWD));128doURL(p, false);129doPwd(p);130sw = new StringWriter();131p.write(sw);132newP = new PolicyParser();133newP.read(new StringReader(sw.toString()));134doURL(p, false);135doPwd(p);136137// test policy keystore + URL + type + provider + password138139p = new PolicyParser();140p.read(new StringReader(POLICY_URL_T_P_PWD));141doURL_T_P(p, false);142doPwd(p);143sw = new StringWriter();144p.write(sw);145newP = new PolicyParser();146newP.read(new StringReader(sw.toString()));147doURL_T_P(p, false);148doPwd(p);149150// test policy password with no keystore151p = new PolicyParser();152try {153p.read(new StringReader(POLICY_PASS_NO_STORE));154throw new SecurityException("expected parsing exception");155} catch (PolicyParser.ParsingException pe) {156// good157}158159}160161private static void checkPerm(PolicyParser p) throws Exception {162Enumeration e = p.grantElements();163boolean foundOne = false;164while (e.hasMoreElements()) {165PolicyParser.GrantEntry ge = (PolicyParser.GrantEntry)166e.nextElement();167if (ge.permissionEntries == null) {168throw new SecurityException("expected non-null perms");169} else {170foundOne = true;171}172}173if (!foundOne) {174throw new SecurityException("expected non-null grant entries");175}176}177178private static void doNoStore(PolicyParser p) throws Exception {179if (p.getKeyStoreUrl() != null ||180p.getKeyStoreType() != null ||181p.getKeyStoreProvider() != null ||182p.getStorePassURL() != null) {183throw new SecurityException("expected null keystore");184}185checkPerm(p);186}187188private static void doURL(PolicyParser p, boolean checkPwd)189throws Exception {190if (p.getKeyStoreUrl() == null ||191!(p.getKeyStoreUrl().endsWith("TokenStore.keystore")) ||192p.getKeyStoreType() != null ||193p.getKeyStoreProvider() != null) {194throw new SecurityException("invalid keystore values");195}196if (checkPwd) {197if (p.getStorePassURL() != null) {198throw new SecurityException("invalid keystore values");199}200}201checkPerm(p);202}203204private static void doURL_T(PolicyParser p, boolean checkPwd)205throws Exception {206if (p.getKeyStoreUrl() == null ||207!(p.getKeyStoreUrl().endsWith("TokenStore.keystore")) ||208p.getKeyStoreType() == null ||209!(p.getKeyStoreType().equals("JKS")) ||210p.getKeyStoreProvider() != null) {211throw new SecurityException("invalid keystore values");212}213if (checkPwd) {214if (p.getStorePassURL() != null) {215throw new SecurityException("invalid keystore values");216}217}218checkPerm(p);219}220221private static void doURL_T_P(PolicyParser p, boolean checkPwd)222throws Exception {223if (p.getKeyStoreUrl() == null ||224!(p.getKeyStoreUrl().endsWith("TokenStore.keystore")) ||225p.getKeyStoreType() == null ||226!(p.getKeyStoreType().equals("JKS")) ||227p.getKeyStoreProvider() == null ||228!(p.getKeyStoreProvider().equals("SUN"))) {229throw new SecurityException("invalid keystore values");230}231if (checkPwd) {232if (p.getStorePassURL() != null) {233throw new SecurityException("invalid keystore values");234}235}236checkPerm(p);237}238239private static void doPwd(PolicyParser p) throws Exception {240if (p.getStorePassURL() == null ||241!(p.getStorePassURL().endsWith("TokenStore.pwd"))) {242throw new SecurityException("invalid password values");243}244}245}246247248