1 8 package org.codehaus.loom.xmlpolicy.runtime; 9 10 import java.net.URL ; 11 import java.security.CodeSource ; 12 import java.security.Permissions ; 13 import java.security.AllPermission ; 14 import java.security.Policy ; 15 import java.security.PermissionCollection ; 16 import java.security.Permission ; 17 import java.security.cert.Certificate ; 18 import java.util.HashMap ; 19 import java.util.Enumeration ; 20 21 import org.codehaus.loom.xmlpolicy.runtime.DefaultPolicy; 22 import org.codehaus.loom.xmlpolicy.runtime.PolicyEntry; 23 24 import junit.framework.TestCase; 25 26 31 public class RuntimeTestCase 32 extends TestCase 33 { 34 public RuntimeTestCase( final String name ) 35 { 36 super( name ); 37 } 38 39 public void testNullCodeSourceInEntryCtor() 40 throws Exception 41 { 42 try 43 { 44 new PolicyEntry( null, new Permissions () ); 45 fail( "Expected to fail due to null pointer in ctor" ); 46 } 47 catch( final NullPointerException npe ) 48 { 49 assertEquals( "NPE message", 50 "codeSource", 51 npe.getMessage() ); 52 } 53 } 54 55 public void testNullPermissionsInEntryCtor() 56 throws Exception 57 { 58 try 59 { 60 new PolicyEntry( new CodeSource ( new URL ( "http://spice.sourveforge.net" ), 61 new Certificate [ 0 ] ), 62 null ); 63 fail( "Expected to fail due to null pointer in ctor" ); 64 } 65 catch( final NullPointerException npe ) 66 { 67 assertEquals( "NPE message", 68 "permissions", 69 npe.getMessage() ); 70 } 71 } 72 73 public void testEntryCtor() 74 throws Exception 75 { 76 final URL url = new URL ( "http://spice.sourveforge.net" ); 77 final CodeSource codeSource = new CodeSource ( url, new Certificate [ 0 ] ); 78 final Permissions permissions = new Permissions (); 79 try 80 { 81 final PolicyEntry entry = new PolicyEntry( codeSource, permissions ); 82 assertEquals( "Entry.getCodeSource", codeSource, entry.getCodeSource() ); 83 assertEquals( "Entry.getPermissions", permissions, entry.getPermissions() ); 84 } 85 catch( final Throwable t ) 86 { 87 fail( "Expected ctor not to except" ); 88 } 89 } 90 91 public void testPolicyAccessPermission() 92 throws Exception 93 { 94 final URL url = new URL ( "file:/-" ); 95 final CodeSource codeSource = new CodeSource ( url, new Certificate [ 0 ] ); 96 final AllPermission allPermission = new AllPermission (); 97 final HashMap grants = new HashMap (); 98 grants.put( codeSource, new Permission []{allPermission} ); 99 100 final Policy policy = new DefaultPolicy( grants ); 101 policy.refresh(); 102 final PermissionCollection resultPermissions = policy.getPermissions( codeSource ); 103 final Enumeration enumeration = resultPermissions.elements(); 104 while( enumeration.hasMoreElements() ) 105 { 106 final Permission permission = (Permission )enumeration.nextElement(); 107 assertEquals( "Permissions for codeSource" + codeSource, 108 allPermission, 109 permission ); 110 return; 111 } 112 fail( "Expected to find AllPermission in set" ); 113 } 114 115 public void testPolicyAccessPermissionNotCovered() 116 throws Exception 117 { 118 final URL url = new URL ( "http://spice.sourceforge.net/-" ); 119 final CodeSource codeSource = new CodeSource ( url, new Certificate [ 0 ] ); 120 final AllPermission allPermission = new AllPermission (); 121 final HashMap grants = new HashMap (); 122 grants.put( codeSource, new Permission []{allPermission} ); 123 124 final Policy policy = new DefaultPolicy( grants ); 125 policy.refresh(); 126 final PermissionCollection resultPermissions = policy.getPermissions( new CodeSource ( null, new Certificate [ 0 ] ) ); 127 final Enumeration enumeration = resultPermissions.elements(); 128 assertEquals( "Permissions for codeSource" + codeSource, 129 false, enumeration.hasMoreElements() ); 130 } 131 132 public void testPolicyAccessPermissionForNonSpecifiedCodeBase() 133 throws Exception 134 { 135 final Policy policy = new DefaultPolicy(); 136 policy.refresh(); 137 138 final URL url = new URL ( "http://spice.sourceforge.net/-" ); 139 final CodeSource codeSource = new CodeSource ( url, new Certificate [ 0 ] ); 140 final PermissionCollection permissions = policy.getPermissions( codeSource ); 141 assertEquals( "Expect no permissions for http://...", false, permissions.elements().hasMoreElements() ); 142 143 final CodeSource otherCodeSource = new CodeSource ( null, new Certificate [ 0 ] ); 144 final PermissionCollection otherPermissions = policy.getPermissions( otherCodeSource ); 145 assertEquals( "Expect no permissions for null location", false, otherPermissions.elements().hasMoreElements() ); 146 } 147 } 148 | Popular Tags |