Path: blob/master/test/jdk/javax/crypto/CryptoPermissions/TestUnlimited.java
41152 views
/*1* Copyright (c) 2016, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/**26* @test27* @bug 806184228* @summary Package jurisdiction policy files as something other than JAR29* @run main/othervm TestUnlimited use_default default30* @run main/othervm TestUnlimited "" exception31* @run main/othervm TestUnlimited limited limited32* @run main/othervm TestUnlimited unlimited unlimited33* @run main/othervm TestUnlimited unlimited/ unlimited34* @run main/othervm TestUnlimited NosuchDir exception35* @run main/othervm TestUnlimited . exception36* @run main/othervm TestUnlimited /tmp/unlimited exception37* @run main/othervm TestUnlimited ../policy/unlimited exception38* @run main/othervm TestUnlimited ./unlimited exception39* @run main/othervm TestUnlimited /unlimited exception40*/41import javax.crypto.*;42import java.security.Security;43import java.nio.file.*;44import java.util.stream.*;4546public class TestUnlimited {4748private enum Result {49UNLIMITED,50LIMITED,51EXCEPTION,52UNKNOWN53};5455/*56* Grab the default policy entry from java.security.57*58* If the input java.security file is malformed59* (missing crypto.policy, attribute/no value, etc), throw60* exception. split() might throw AIOOB which61* is ok behavior.62*/63private static String getDefaultPolicy() throws Exception {64String javaHome = System.getProperty("java.home");65Path path = Paths.get(javaHome, "conf", "security", "java.security");6667try (Stream<String> lines = Files.lines(path)) {68return lines.filter(x -> x.startsWith("crypto.policy="))69.findFirst().orElseThrow(70() -> new Exception("Missing crypto.policy"))71.split("=")[1].trim();72}73}7475public static void main(String[] args) throws Exception {76/*77* Override the Security property to allow for unlimited policy.78* Would need appropriate permissions if Security Manager were79* active.80*/81if (args.length != 2) {82throw new Exception("Two args required");83}8485String testStr = args[0];86String expectedStr = args[1];87if (testStr.equals("use_default")) {88expectedStr = getDefaultPolicy();89}9091Result expected = Result.UNKNOWN; // avoid NPE warnings92Result result;9394switch (expectedStr) {95case "unlimited":96expected = Result.UNLIMITED;97break;98case "limited":99expected = Result.LIMITED;100break;101case "exception":102expected = Result.EXCEPTION;103break;104default:105throw new Exception("Unexpected argument");106}107108System.out.println("Testing: " + testStr);109if (testStr.equals("\"\"")) {110Security.setProperty("crypto.policy", "");111} else {112// skip default case.113if (!testStr.equals("use_default")) {114Security.setProperty("crypto.policy", testStr);115}116}117118/*119* Use the AES as the test Cipher120* If there is an error initializing, we will never get past here.121*/122try {123int maxKeyLen = Cipher.getMaxAllowedKeyLength("AES");124System.out.println("max AES key len:" + maxKeyLen);125if (maxKeyLen > 128) {126System.out.println("Unlimited policy is active");127result = Result.UNLIMITED;128} else {129System.out.println("Unlimited policy is NOT active");130result = Result.LIMITED;131}132} catch (Throwable e) {133//ExceptionInInitializerError's134result = Result.EXCEPTION;135}136137System.out.println(138"Expected:\t" + expected + "\nResult:\t\t" + result);139if (!expected.equals(result)) {140throw new Exception("Didn't match");141}142143System.out.println("DONE!");144}145}146147148