/test/java/security/Policy/GetInstance/GetInstance.java

https://github.com/ikeji/openjdk7-jdk · Java · 248 lines · 148 code · 42 blank · 58 comment · 8 complexity · 6a582e5cee275498285bfe8bae61646a MD5 · raw file

  1. /*
  2. * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  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
  7. * published by the Free Software Foundation.
  8. *
  9. * This code is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * version 2 for more details (a copy is included in the LICENSE file that
  13. * accompanied this code).
  14. *
  15. * You should have received a copy of the GNU General Public License version
  16. * 2 along with this work; if not, write to the Free Software Foundation,
  17. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20. * or visit www.oracle.com if you need additional information or have any
  21. * questions.
  22. */
  23. /*
  24. * @test
  25. * @bug 5100561
  26. * @bug 6273812
  27. * @summary Can not explicitly create a java.security.Policy object from a file
  28. * @build GetInstancePolicySpi GetInstanceProvider
  29. * @run main/othervm/policy=GetInstance.policy GetInstance
  30. */
  31. import java.security.*;
  32. import java.io.File;
  33. import java.net.URI;
  34. import sun.net.www.ParseUtil;
  35. public class GetInstance {
  36. private static final String JAVA_POLICY = "JavaPolicy";
  37. private static class BadParam implements Policy.Parameters { }
  38. public static void main(String[] args) throws Exception {
  39. int testnum = 1;
  40. GetInstance gi = new GetInstance();
  41. testnum = gi.testDefault(testnum);
  42. testnum = gi.testStringProvider(testnum);
  43. testnum = gi.testProvider(testnum);
  44. testnum = gi.testCustomImpl(testnum);
  45. testnum = gi.testBadParam(testnum);
  46. // make this go last because we don't want to leave its policy set
  47. // for other tests
  48. testnum = gi.testURIParam(testnum);
  49. }
  50. private int testDefault(int testnum) throws Exception {
  51. // get an instance of the default PolicySpiFile
  52. Policy p = Policy.getInstance(JAVA_POLICY, null);
  53. doTest(p, testnum++);
  54. Policy.setPolicy(p);
  55. doTestSM(testnum++);
  56. // get an instance of FooPolicy
  57. try {
  58. p = Policy.getInstance("FooPolicy", null);
  59. throw new SecurityException("test " + testnum++ + " failed");
  60. } catch (NoSuchAlgorithmException nsae) {
  61. // good
  62. System.out.println("test " + testnum++ + " passed");
  63. }
  64. return testnum;
  65. }
  66. private int testStringProvider(int testnum) throws Exception {
  67. // get an instance of JavaPolicy from SUN
  68. Policy p = Policy.getInstance(JAVA_POLICY, null, "SUN");
  69. doTest(p, testnum++);
  70. Policy.setPolicy(p);
  71. doTestSM(testnum++);
  72. // get an instance of JavaPolicy from SunRsaSign
  73. try {
  74. p = Policy.getInstance(JAVA_POLICY, null, "SunRsaSign");
  75. throw new SecurityException("test " + testnum++ + " failed");
  76. } catch (NoSuchAlgorithmException nsae) {
  77. // good
  78. System.out.println("test " + testnum++ + " passed");
  79. }
  80. // get an instance of JavaPolicy from FOO
  81. try {
  82. p = Policy.getInstance(JAVA_POLICY, null, "FOO");
  83. throw new SecurityException("test " + testnum++ + " failed");
  84. } catch (NoSuchProviderException nspe) {
  85. // good
  86. System.out.println("test " + testnum++ + " passed");
  87. }
  88. return testnum;
  89. }
  90. private int testProvider(int testnum) throws Exception {
  91. // get an instance of JavaPolicy from SUN
  92. Policy p = Policy.getInstance(JAVA_POLICY,
  93. null,
  94. Security.getProvider("SUN"));
  95. doTest(p, testnum++);
  96. Policy.setPolicy(p);
  97. doTestSM(testnum++);
  98. // get an instance of JavaPolicy from SunRsaSign
  99. try {
  100. p = Policy.getInstance(JAVA_POLICY,
  101. null,
  102. Security.getProvider("SunRsaSign"));
  103. throw new SecurityException("test " + testnum++ + " failed");
  104. } catch (NoSuchAlgorithmException nsae) {
  105. // good
  106. System.out.println("test " + testnum++ + " passed");
  107. }
  108. return testnum;
  109. }
  110. private int testBadParam(int testnum) throws Exception {
  111. // pass bad param
  112. try {
  113. Policy p = Policy.getInstance(JAVA_POLICY,
  114. new BadParam());
  115. throw new SecurityException("test " + testnum++ + " failed");
  116. } catch (IllegalArgumentException iae) {
  117. // good
  118. System.out.println("test " + testnum++ + " passed");
  119. }
  120. try {
  121. Policy p = Policy.getInstance(JAVA_POLICY,
  122. new BadParam(),
  123. "SUN");
  124. throw new SecurityException("test " + testnum++ + " failed");
  125. } catch (IllegalArgumentException iae) {
  126. // good
  127. System.out.println("test " + testnum++ + " passed");
  128. }
  129. try {
  130. Policy p = Policy.getInstance(JAVA_POLICY,
  131. new BadParam(),
  132. Security.getProvider("SUN"));
  133. throw new SecurityException("test " + testnum++ + " failed");
  134. } catch (IllegalArgumentException iae) {
  135. // good
  136. System.out.println("test " + testnum++ + " passed");
  137. }
  138. return testnum;
  139. }
  140. private int testURIParam(int testnum) throws Exception {
  141. // get an instance of JavaPolicy from SUN and have it read from the URL
  142. File file = new File(System.getProperty("test.src", "."),
  143. "GetInstance.policyURL");
  144. URI uri = file.toURI();
  145. Policy p = Policy.getInstance(JAVA_POLICY, new URIParameter(uri));
  146. doTest(p, testnum++);
  147. Policy.setPolicy(p);
  148. doTestSM(testnum++);
  149. return testnum;
  150. }
  151. private int testCustomImpl(int testnum) throws Exception {
  152. Provider customProvider = new GetInstanceProvider();
  153. Policy p = Policy.getInstance("GetInstancePolicySpi",
  154. null,
  155. customProvider);
  156. // doTest has a case that will not work with custom policies,
  157. // so do not call it
  158. //
  159. // doTest(p, testnum++);
  160. Policy.setPolicy(p);
  161. doTestSM(testnum++);
  162. return testnum;
  163. }
  164. private void doTest(Policy p, int testnum) throws Exception {
  165. // check granted perm
  166. if (p.implies(this.getClass().getProtectionDomain(),
  167. new SecurityPermission("GetInstanceTest"))) {
  168. System.out.println("test " + testnum + ".1 passed");
  169. } else {
  170. throw new SecurityException("test " + testnum + ".1 failed");
  171. }
  172. // check perm not granted
  173. if (p.implies(this.getClass().getProtectionDomain(),
  174. new SecurityPermission("NotGranted"))) {
  175. throw new SecurityException("test " + testnum + ".2 failed");
  176. } else {
  177. System.out.println("test " + testnum + ".2 passed");
  178. }
  179. // test getProvider
  180. if ("SUN".equals(p.getProvider().getName())) {
  181. System.out.println("test " + testnum + ".3 passed");
  182. } else {
  183. throw new SecurityException("test " + testnum + ".3 failed");
  184. }
  185. // test getType
  186. if (JAVA_POLICY.equals(p.getType())) {
  187. System.out.println("test " + testnum + ".4 passed");
  188. } else {
  189. throw new SecurityException("test " + testnum + ".4 failed");
  190. }
  191. }
  192. private void doTestSM(int testnum) throws Exception {
  193. // check granted perm
  194. System.getSecurityManager().checkPermission
  195. (new SecurityPermission("GetInstanceTest"));
  196. System.out.println("test " + testnum + ".1 passed");
  197. // check perm not granted
  198. try {
  199. System.getSecurityManager().checkPermission
  200. (new SecurityPermission("NotGranted"));
  201. throw new RuntimeException("test " + testnum + ".2 failed");
  202. } catch (SecurityException se) {
  203. System.out.println("test " + testnum + ".2 passed");
  204. }
  205. }
  206. }