/luni/src/test/java/org/apache/harmony/security/tests/java/security/Permission2Test.java

https://github.com/MIPS/libcore · Java · 166 lines · 111 code · 16 blank · 39 comment · 0 complexity · 445d7d97f952292960832d14b0d692cc MD5 · raw file

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.harmony.security.tests.java.security;
  18. import dalvik.annotation.TestTargetClass;
  19. import dalvik.annotation.TestTargets;
  20. import dalvik.annotation.TestLevel;
  21. import dalvik.annotation.TestTargetNew;
  22. import java.security.Permission;
  23. import java.security.PermissionCollection;
  24. import java.security.SecurityPermission;
  25. @TestTargetClass(Permission.class)
  26. public class Permission2Test extends junit.framework.TestCase {
  27. static class ConcretePermission extends Permission {
  28. public ConcretePermission() {
  29. super("noname");
  30. }
  31. public boolean equals(Object obj) {
  32. return true;
  33. }
  34. public String getActions() {
  35. return "none";
  36. }
  37. public int hashCode() {
  38. return 1;
  39. }
  40. public boolean implies(Permission p) {
  41. return true;
  42. }
  43. }
  44. /**
  45. * @tests java.security.Permission#Permission(java.lang.String)
  46. */
  47. @TestTargetNew(
  48. level = TestLevel.COMPLETE,
  49. notes = "",
  50. method = "Permission",
  51. args = {java.lang.String.class}
  52. )
  53. public void test_ConstructorLjava_lang_String() {
  54. // test method java.security.permission.Permission(string)
  55. try {
  56. SecurityPermission permi = new SecurityPermission(
  57. "Testing the permission abstract class");
  58. String name = permi.getName();
  59. assertEquals("Permission Constructor failed",
  60. "Testing the permission abstract class", name);
  61. } catch (Exception e) {
  62. fail("Unexpected excpetion");
  63. }
  64. try {
  65. SecurityPermission permi = new SecurityPermission(null);
  66. fail("NullPointerException was not thrown for NULL parameter");
  67. } catch (NullPointerException e) {
  68. //expected
  69. }
  70. try {
  71. SecurityPermission permi = new SecurityPermission("");
  72. fail("IllegalArgumentException was not thrown for empty parameter");
  73. } catch (IllegalArgumentException e) {
  74. //expected
  75. }
  76. }
  77. /**
  78. * @tests java.security.Permission#checkGuard(java.lang.Object)
  79. */
  80. @TestTargetNew(
  81. level = TestLevel.PARTIAL_COMPLETE,
  82. notes = "",
  83. method = "checkGuard",
  84. args = {java.lang.Object.class}
  85. )
  86. public void test_checkGuardLjava_lang_Object() {
  87. // test method java.security.permission.checkGuard(object)
  88. SecurityPermission permi = new SecurityPermission(
  89. "Testing the permission abstract class");
  90. String name = permi.getName();
  91. try {
  92. permi.checkGuard(name);
  93. } catch (SecurityException e) {
  94. fail("security not granted when it is suppose to be : " + e);
  95. }
  96. }
  97. /**
  98. * @tests java.security.Permission#getName()
  99. */
  100. @TestTargetNew(
  101. level = TestLevel.COMPLETE,
  102. notes = "",
  103. method = "getName",
  104. args = {}
  105. )
  106. public void test_getName() {
  107. // test method java.security.permission.getName()
  108. SecurityPermission permi = new SecurityPermission("testing getName()");
  109. String name = permi.getName();
  110. assertEquals("getName failed to obtain the correct name",
  111. "testing getName()", name);
  112. SecurityPermission permi2 = new SecurityPermission("93048Helloworld");
  113. assertEquals("getName failed to obtain correct name",
  114. "93048Helloworld", permi2.getName());
  115. }
  116. /**
  117. * @tests java.security.Permission#newPermissionCollection()
  118. */
  119. @TestTargetNew(
  120. level = TestLevel.COMPLETE,
  121. notes = "",
  122. method = "newPermissionCollection",
  123. args = {}
  124. )
  125. public void test_newPermissionCollection() {
  126. // test method java.security.permission.newPermissionCollection
  127. Permission permi = new ConcretePermission();
  128. PermissionCollection permiCollect = permi.newPermissionCollection();
  129. assertNull("newPermissionCollector of the abstract class "
  130. + "permission did not return a null instance "
  131. + "of permissionCollection", permiCollect);
  132. }
  133. /**
  134. * @tests java.security.Permission#toString()
  135. */
  136. @TestTargetNew(
  137. level = TestLevel.COMPLETE,
  138. notes = "",
  139. method = "toString",
  140. args = {}
  141. )
  142. public void test_toString() {
  143. // test method java.security.permission.toString
  144. // test for permission with no action
  145. SecurityPermission permi = new SecurityPermission("testing toString");
  146. String toString = permi.toString();
  147. assertNotNull("toString should have returned a string of elements",
  148. toString);
  149. }
  150. }