1 /* |
1 /* |
2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
31 import java.io.*; |
31 import java.io.*; |
32 import java.lang.reflect.*; |
32 import java.lang.reflect.*; |
33 |
33 |
34 public class ConfigShortPath { |
34 public class ConfigShortPath { |
35 |
35 |
36 private static final String[] configNames = { |
36 private static final String[] winConfigNames = { |
37 "csp.cfg", "cspPlus.cfg", "cspSpace.cfg", "cspQuotedPath.cfg" |
37 "csp.cfg", "cspSpace.cfg", "cspQuotedPath.cfg" |
|
38 }; |
|
39 private static final String[] solConfigNames = { |
|
40 "cspPlus.cfg" |
38 }; |
41 }; |
39 |
42 |
40 public static void main(String[] args) throws Exception { |
43 public static void main(String[] args) throws Exception { |
41 Constructor cons = null; |
44 Provider p = Security.getProvider("SunPKCS11"); |
42 try { |
45 if (p == null) { |
43 Class clazz = Class.forName("sun.security.pkcs11.SunPKCS11"); |
46 // re-try w/ SunPKCS11-Solaris |
44 cons = clazz.getConstructor(String.class); |
47 p = Security.getProvider("SunPKCS11-Solaris"); |
45 } catch (Exception ex) { |
48 if (p == null) { |
46 System.out.println("Skipping test - no PKCS11 provider available"); |
49 System.out.println("Skipping test - no PKCS11 provider available"); |
47 return; |
50 return; |
|
51 } |
48 } |
52 } |
|
53 |
|
54 String osInfo = System.getProperty("os.name", ""); |
|
55 String[] configNames = (osInfo.contains("Windows")? |
|
56 winConfigNames : solConfigNames); |
|
57 |
49 String testSrc = System.getProperty("test.src", "."); |
58 String testSrc = System.getProperty("test.src", "."); |
50 for (int i = 0; i < configNames.length; i++) { |
59 for (int i = 0; i < configNames.length; i++) { |
51 String configFile = testSrc + File.separator + configNames[i]; |
60 String configFile = testSrc + File.separator + configNames[i]; |
52 |
61 |
53 System.out.println("Testing against " + configFile); |
62 System.out.println("Testing against " + configFile); |
54 try { |
63 try { |
55 Object obj = cons.newInstance(configFile); |
64 p.configure(configFile); |
56 } catch (InvocationTargetException ite) { |
65 } catch (InvalidParameterException ipe) { |
57 Throwable cause = ite.getCause(); |
66 ipe.printStackTrace(); |
58 System.out.println(cause); |
67 Throwable cause = ipe.getCause(); |
59 if (cause instanceof ProviderException) { |
68 // Indicate failure if due to parsing config |
60 while ((cause = cause.getCause()) != null) { |
69 if (cause.getClass().getName().equals |
61 System.out.println(cause); |
70 ("sun.security.pkcs11.ConfigurationException")) { |
62 String causeMsg = cause.getMessage(); |
71 // Error occurred during parsing |
63 // Indicate failure if due to parsing config |
72 if (cause.getMessage().indexOf("Unexpected") != -1) { |
64 if (causeMsg.indexOf("Unexpected") != -1) { |
73 throw (ProviderException) cause; |
65 throw (ProviderException) cause; |
|
66 } |
|
67 } |
74 } |
68 // Consider the test passes if the exception is |
75 } |
69 // thrown after parsing, i.e. due to the absolute |
76 } catch (ProviderException pe) { |
70 // path requirement or the non-existent path. |
77 pe.printStackTrace(); |
71 } else { |
78 if (pe.getCause() instanceof IOException) { |
72 // unexpected exception |
79 // Thrown when the directory does not exist which is ok |
73 throw new RuntimeException("Unexpected Exception", cause); |
80 System.out.println("Pass: config parsed ok"); |
|
81 continue; |
74 } |
82 } |
75 } |
83 } |
76 } |
84 } |
77 } |
85 } |
78 } |
86 } |