/** * two BasicPermissions are equal if name and class are equal; * equal permissions should have the same hash code */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "equals", args = {java.lang.Object.class} ) public void testEquals() { BasicPermission b1 = new BasicPermissionImpl("abc"); BasicPermission b2 = null; assertTrue(b1.equals(b1)); assertFalse(b1.equals(null)); assertFalse(b1.equals(new Object())); assertFalse(b1.equals("abc")); assertTrue(b1.equals(b2 = new BasicPermissionImpl("abc"))); assertTrue(b1.hashCode() == b2.hashCode()); assertFalse(b1.equals(new BasicPermission("abc"){})); assertFalse(b1.equals(new BasicPermissionImpl("abc.*"))); }
/** * implies() should return true if a permission is equal to or is implied * by wildcarded permission, false otherwise. */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "implies", args = {java.security.Permission.class} ) public void testImplies() { BasicPermission b1 = new BasicPermissionImpl("a.b.c"); assertTrue(b1.implies(b1)); assertTrue(b1.implies(new BasicPermissionImpl("a.b.c"))); assertFalse(b1.implies(new BasicPermissionImpl("a.b.c.*"))); assertFalse(b1.implies(new BasicPermission("a.b.c"){})); assertTrue(new BasicPermissionImpl("a.b.*").implies(b1)); assertTrue(new BasicPermissionImpl("a.*").implies(b1)); assertTrue(new BasicPermissionImpl("*").implies(b1)); assertFalse(new BasicPermissionImpl("a.b*").implies(b1)); assertFalse(new BasicPermissionImpl("a.b.c.*").implies(b1)); assertTrue(new BasicPermissionImpl("1.*").implies(new BasicPermissionImpl("1.234.*"))); assertTrue(new BasicPermissionImpl("*").implies(new BasicPermissionImpl("*"))); }
/** Any of AllPermission instances are equal and have the same hash code */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "equals", args = {java.lang.Object.class} ) public void testEquals() { AllPermission a1 = new AllPermission(); AllPermission a2 = new AllPermission(); assertTrue(a1.equals(a2)); assertTrue(a1.hashCode() == a2.hashCode()); assertFalse(a1.equals(null)); assertFalse(a1.equals(new BasicPermission("hgf"){})); }
public static void main(String[] args) throws Exception { System.setSecurityManager(new SecurityManager()); java.net.URL[] urls = new java.net.URL[1]; Class<?> c = null; java.net.URLClassLoader ucl = null; try { URL url = new URL(args[0]); urls[0] = url; ucl = URLClassLoader.newInstance(urls, null); c = Class.forName("coucou.FileAccess", true, ucl); } catch (Exception e) { e.printStackTrace(); } ProtectionDomain pd = c.getProtectionDomain(); PermissionCollection coll = Policy.getPolicy().getPermissions(pd); Class<?> myPermission = Class.forName("mypackage.MyPermission"); BasicPermission myperm = (BasicPermission) myPermission.newInstance(); System.out.println(coll.implies(new java.io.FilePermission("test1.txt", "write")) + "," + coll.implies(myperm) + "," + coll .implies(new java.io.FilePermission("test2.txt", "write")) + ","); }
public static void main(String[] args) throws Exception { BasicPermission bp1 = new BP("exitVM"); BasicPermission bp2 = new BP("exitVM.*"); StringBuffer sb = new StringBuffer(); // First, make sure the old restrictions on exitVM and exitVM.* still hold. if (!bp1.implies(bp2)) sb.append("bp1 does not implies bp2\n"); if (!bp2.implies(bp1)) sb.append("bp2 does not implies bp1\n"); // Test against hashCode spec if (bp1.hashCode() != bp1.getName().hashCode()) sb.append("bp1 hashCode not spec consistent\n"); if (bp2.hashCode() != bp2.getName().hashCode()) sb.append("bp2 hashCode not spec consistent\n"); // Test against equals spec if (bp1.getName().equals(bp2.getName())) { if (!bp1.equals(bp2)) { sb.append("BP breaks equals spec\n"); } } if (!bp1.getName().equals(bp2.getName())) { if (bp1.equals(bp2)) { sb.append("BP breaks equals spec in another way\n"); } } // Tests against common knowledge: If equals, then hashCode should be same if (bp1.equals(bp2)) { if (bp1.hashCode() != bp2.hashCode()) { sb.append("Equal objects have unequal hashCode?\n"); } } if (sb.length() > 0) { throw new Exception(sb.toString()); } }
public static void main (String argv[]) throws Exception { try { AccessController.checkPermission( new BasicPermission("no such permission"){}); } catch (NullPointerException npe) { throw new Exception("Unexpected NullPointerException for security" + " debug option, -Djava.security.debug=failure"); } catch (AccessControlException ace) { } }
/** * Can add any type of permissions. Cannot add if collection is read-only. */ @TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, notes = "", method = "add", args = {java.security.Permission.class} ) public void testAdd() { Permissions ps = new Permissions(); Permission ap = new AllPermission(); Permission bp = new BasicPermission("jhb23jhg5") { }; Permission sp0 = new SecurityPermission("abc"); Permission sp1 = new SecurityPermission("a.b.c"); Permission sp2 = new SecurityPermission("a.b.*"); Permission sp3 = new SecurityPermission("a.*"); Permission up1 = new UnresolvedPermission("131234", null, null, null); Permission up2 = new UnresolvedPermission("KUJKHVKJgyuygjhb", "xcv456", "26r ytf", new java.security.cert.Certificate[0]); Permission[] arr = new Permission[] { up1, up2, ap, bp, sp0, sp1, sp2, sp3, }; for (int i = 0; i < arr.length; i++) { ps.add(arr[i]); } //test add duplicate ps.add(up1); ps.add(sp0); ps.setReadOnly(); try { ps.add(up1); fail("read-only flag is ignored"); } catch (SecurityException ok) { } }
/** * newPermissionCollection() should return new BasicPermissionCollection on every invocation */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "newPermissionCollection", args = {} ) public void testCollection() { BasicPermission b1 = new BasicPermissionImpl("a.b.c"); PermissionCollection pc1 = b1.newPermissionCollection(); PermissionCollection pc2 = b1.newPermissionCollection(); // assertTrue((pc1 instanceof BasicPermissionCollection) && (pc2 instanceof BasicPermissionCollection)); assertNotSame(pc1, pc2); }
/** AllPermission implies any other permission */ @TestTargetNew( level = TestLevel.PARTIAL, notes = "Null parameter checking missed", method = "implies", args = {java.security.Permission.class} ) public void testImplies() { AllPermission a1 = new AllPermission(); assertTrue(a1.implies(new AllPermission())); assertTrue(a1.implies(new BasicPermission("2323"){})); assertTrue(a1.implies(new UnresolvedPermission("2323", "", "", null))); }
@SuppressWarnings("serial") public void testEquals() { DelegationPermission dp1 = new DelegationPermission("\"AAA\" \"BBB\""); DelegationPermission dp2 = new DelegationPermission("\"AAA\" \"BBB\""); assertTrue(dp1.equals(dp1)); assertFalse(dp1.equals(new DelegationPermission("\"aaa\" \"bbb\""))); assertTrue(dp2.equals(dp1)); assertTrue(dp1.equals(dp2)); assertTrue(dp1.hashCode() == dp2.hashCode()); assertFalse(dp1.equals(new BasicPermission("\"AAA\""){})); }
public static void main(String[] args) throws Exception { System.setSecurityManager(new SecurityManager()); java.net.URL[] urls = new java.net.URL[1]; Class<?> c = null; java.net.URLClassLoader ucl = null; try { URL url = new URL(args[0]); urls[0] = url; ucl = URLClassLoader.newInstance(urls, null); c = Class.forName("coucou.FileAccess", true, ucl); } catch (Exception e) { e.printStackTrace(); } ProtectionDomain pd = c.getProtectionDomain(); PermissionCollection coll = Policy.getPolicy().getPermissions(pd); Class<?> myPermission = Class.forName("mypackage.MyPermission"); BasicPermission myperm = (BasicPermission) myPermission.newInstance(); System.out.println( coll.implies(new java.io.FilePermission("test1.txt", "write")) + "," + coll.implies(myperm) + "," + coll.implies(new java.io.FilePermission("test2.txt", "write")) + "," + coll.implies(new java.io.FilePermission("test3.txt", "read")) + ","); }
public ProtectionDomain[] combine(ProtectionDomain[] currentDomains, ProtectionDomain[] assignedDomains) { log.info("MyDomainCombiner.combine() has been called"); if (currentDomains[0] == null) { throw new RuntimeException ("A protection domain of the main execution thread is null. Test failed."); } //log.info("A " + (index + 1) + " call for DomainCombiner.combine() method"); ProtectionDomain originalPD = currentDomains[1]; PermissionCollection perms = AccessTestClass.policy.getPermissions(originalPD); //log.info("A set of permissions of the protection domain of the main execution thread is:"); //log.info(perms); if (!perms.isReadOnly()) { BasicPermission p = new PropertyPermission(propertyName, "read"); log.info("A permission which will be added: " + p+ "\n"); perms.add(p); perms.setReadOnly(); } //currentDomains[1] = new ProtectionDomain(originalPD.getCodeSource(), perms, originalPD.getClassLoader(), null); ProtectionDomain[] pd = new ProtectionDomain[1]; pd[0] = new ProtectionDomain(originalPD.getCodeSource(), perms, originalPD.getClassLoader(), null); // dummyCurrentDomains[index] = currentDomains; // dummyAssignedDomains[index] = assignedDomains; //index++; //return currentDomains; return pd; }
/** * Should return non-null empty enumeration for empty collection. For * non-empty collection, should always return enumeration over unique * elements. */ @TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, notes = "", method = "elements", args = {} ) public void testElements() { Permissions ps = new Permissions(); Permission ap = new AllPermission(); Permission bp = new BasicPermission("jhb23jhg5") { public PermissionCollection newPermissionCollection() { return null; } }; Permission sp = new SecurityPermission("abc"); Permission up1 = new UnresolvedPermission("131234", null, null, null); Permission up2 = new UnresolvedPermission("KUJKHVKJgyuygjhb", "xcv456", "26r ytf", new java.security.cert.Certificate[0]); Enumeration<Permission> en = ps.elements(); assertNotNull(en); assertFalse(en.hasMoreElements()); ps.add(up1); en = ps.elements(); assertTrue(en.hasMoreElements()); assertTrue(up1.equals(en.nextElement())); assertFalse(en.hasMoreElements()); ps.add(up1); en = ps.elements(); assertTrue(en.hasMoreElements()); assertTrue(up1.equals(en.nextElement())); //assertFalse(en.hasMoreElements()); Permission[] arr = new Permission[] { ap, bp, sp, up1, up2 }; for (int i = 0; i < arr.length; i++) { ps.add(arr[i]); } en = ps.elements(); Collection<Permission> els = new ArrayList<Permission>(); while (en.hasMoreElements()) { els.add(en.nextElement()); } //assertEquals(5, els.size()); assertTrue(els.containsAll(Arrays.asList(arr))); }