jdk/test/javax/crypto/CryptoPermissions/TestUnlimited.java
changeset 42365 5e640c2994d6
parent 40565 3ac0ba151e70
equal deleted inserted replaced
42364:c5a725b3d358 42365:5e640c2994d6
    25 
    25 
    26 /**
    26 /**
    27  * @test
    27  * @test
    28  * @bug 8061842
    28  * @bug 8061842
    29  * @summary Package jurisdiction policy files as something other than JAR
    29  * @summary Package jurisdiction policy files as something other than JAR
       
    30  * @run main/othervm TestUnlimited use_default default
    30  * @run main/othervm TestUnlimited "" exception
    31  * @run main/othervm TestUnlimited "" exception
    31  * @run main/othervm TestUnlimited limited fail
    32  * @run main/othervm TestUnlimited limited limited
    32  * @run main/othervm TestUnlimited unlimited pass
    33  * @run main/othervm TestUnlimited unlimited unlimited
    33  * @run main/othervm TestUnlimited unlimited/ pass
    34  * @run main/othervm TestUnlimited unlimited/ unlimited
    34  * @run main/othervm TestUnlimited NosuchDir exception
    35  * @run main/othervm TestUnlimited NosuchDir exception
    35  * @run main/othervm TestUnlimited . exception
    36  * @run main/othervm TestUnlimited . exception
    36  * @run main/othervm TestUnlimited /tmp/unlimited exception
    37  * @run main/othervm TestUnlimited /tmp/unlimited exception
    37  * @run main/othervm TestUnlimited ../policy/unlimited exception
    38  * @run main/othervm TestUnlimited ../policy/unlimited exception
    38  * @run main/othervm TestUnlimited ./unlimited exception
    39  * @run main/othervm TestUnlimited ./unlimited exception
    39  * @run main/othervm TestUnlimited /unlimited exception
    40  * @run main/othervm TestUnlimited /unlimited exception
    40  */
    41  */
    41 import javax.crypto.*;
    42 import javax.crypto.*;
    42 import java.security.Security;
    43 import java.security.Security;
       
    44 import java.nio.file.*;
       
    45 import java.util.stream.*;
    43 
    46 
    44 public class TestUnlimited {
    47 public class TestUnlimited {
       
    48 
       
    49     private enum Result {
       
    50         UNLIMITED,
       
    51         LIMITED,
       
    52         EXCEPTION,
       
    53         UNKNOWN
       
    54     };
       
    55 
       
    56     /*
       
    57      * Grab the default policy entry from java.security.
       
    58      *
       
    59      * If the input java.security file is malformed
       
    60      * (missing crypto.policy, attribute/no value, etc), throw
       
    61      * exception.  split() might throw AIOOB which
       
    62      * is ok behavior.
       
    63      */
       
    64     private static String getDefaultPolicy() throws Exception {
       
    65         String javaHome = System.getProperty("java.home");
       
    66         Path path = Paths.get(javaHome, "conf", "security", "java.security");
       
    67 
       
    68         try (Stream<String> lines = Files.lines(path)) {
       
    69             return lines.filter(x -> x.startsWith("crypto.policy="))
       
    70                     .findFirst().orElseThrow(
       
    71                             () -> new Exception("Missing crypto.policy"))
       
    72                     .split("=")[1].trim();
       
    73         }
       
    74     }
    45 
    75 
    46     public static void main(String[] args) throws Exception {
    76     public static void main(String[] args) throws Exception {
    47         /*
    77         /*
    48          * Override the Security property to allow for unlimited policy.
    78          * Override the Security property to allow for unlimited policy.
    49          * Would need appropriate permissions if Security Manager were
    79          * Would need appropriate permissions if Security Manager were
    51          */
    81          */
    52         if (args.length != 2) {
    82         if (args.length != 2) {
    53             throw new Exception("Two args required");
    83             throw new Exception("Two args required");
    54         }
    84         }
    55 
    85 
    56         boolean expected = args[1].equals("pass");
    86         String testStr = args[0];
    57         boolean exception = args[1].equals("exception");
    87         String expectedStr = args[1];
    58         boolean result = false;
    88         if (testStr.equals("use_default")) {
       
    89             expectedStr = getDefaultPolicy();
       
    90         }
    59 
    91 
    60         System.out.println("Testing: " + args[0]);
    92         Result expected = Result.UNKNOWN;  // avoid NPE warnings
       
    93         Result result;
    61 
    94 
    62         if (args[0].equals("\"\"")) {
    95         switch (expectedStr) {
       
    96         case "unlimited":
       
    97             expected = Result.UNLIMITED;
       
    98             break;
       
    99         case "limited":
       
   100             expected = Result.LIMITED;
       
   101             break;
       
   102         case "exception":
       
   103             expected = Result.EXCEPTION;
       
   104             break;
       
   105         default:
       
   106             throw new Exception("Unexpected argument");
       
   107         }
       
   108 
       
   109         System.out.println("Testing: " + testStr);
       
   110         if (testStr.equals("\"\"")) {
    63             Security.setProperty("crypto.policy", "");
   111             Security.setProperty("crypto.policy", "");
    64         } else {
   112         } else {
    65             Security.setProperty("crypto.policy", args[0]);
   113             // skip default case.
       
   114             if (!testStr.equals("use_default")) {
       
   115                 Security.setProperty("crypto.policy", testStr);
       
   116             }
    66         }
   117         }
    67 
   118 
    68         /*
   119         /*
    69          * Use the AES as the test Cipher
   120          * Use the AES as the test Cipher
    70          * If there is an error initializing, we will never get past here.
   121          * If there is an error initializing, we will never get past here.
    72         try {
   123         try {
    73             int maxKeyLen = Cipher.getMaxAllowedKeyLength("AES");
   124             int maxKeyLen = Cipher.getMaxAllowedKeyLength("AES");
    74             System.out.println("max AES key len:" + maxKeyLen);
   125             System.out.println("max AES key len:" + maxKeyLen);
    75             if (maxKeyLen > 128) {
   126             if (maxKeyLen > 128) {
    76                 System.out.println("Unlimited policy is active");
   127                 System.out.println("Unlimited policy is active");
    77                 result = true;
   128                 result = Result.UNLIMITED;
    78             } else {
   129             } else {
    79                 System.out.println("Unlimited policy is NOT active");
   130                 System.out.println("Unlimited policy is NOT active");
    80                 result = false;
   131                 result = Result.LIMITED;
    81             }
   132             }
    82         } catch (Throwable e) {
   133         } catch (Throwable e) {
    83             if (!exception) {
   134             //ExceptionInInitializerError's
    84                 throw new Exception();
   135             result = Result.EXCEPTION;
    85             }
       
    86         }
   136         }
    87 
   137 
    88         System.out.println(
   138         System.out.println(
    89                 "Expected:\t" + expected + "\nResult:\t\t" + result);
   139                 "Expected:\t" + expected + "\nResult:\t\t" + result);
    90         if (expected != result) {
   140         if (!expected.equals(result)) {
    91             throw new Exception();
   141             throw new Exception("Didn't match");
    92         }
   142         }
    93 
   143 
    94         System.out.println("DONE!");
   144         System.out.println("DONE!");
    95     }
   145     }
    96 }
   146 }