1 /*
2 * Copyright (C) The Spice Group. All rights reserved.
3 *
4 * This software is published under the terms of the Spice
5 * Software License version 1.1, a copy of which has been included
6 * with this distribution in the LICENSE.txt file.
7 */
8 package org.codehaus.spice.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 import junit.framework.TestCase;
21
22 /***
23 * TestCase for Runtime package.
24 *
25 * @author Peter Donald
26 */
27 public class RuntimeTestCase
28 extends TestCase
29 {
30 public RuntimeTestCase( final String name )
31 {
32 super( name );
33 }
34
35 public void testNullCodeSourceInEntryCtor()
36 throws Exception
37 {
38 try
39 {
40 new PolicyEntry( null, new Permissions() );
41 fail( "Expected to fail due to null pointer in ctor" );
42 }
43 catch( final NullPointerException npe )
44 {
45 assertEquals( "NPE message",
46 "codeSource",
47 npe.getMessage() );
48 }
49 }
50
51 public void testNullPermissionsInEntryCtor()
52 throws Exception
53 {
54 try
55 {
56 new PolicyEntry( new CodeSource( new URL( "http://spice.sourveforge.net" ),
57 new Certificate[ 0 ] ),
58 null );
59 fail( "Expected to fail due to null pointer in ctor" );
60 }
61 catch( final NullPointerException npe )
62 {
63 assertEquals( "NPE message",
64 "permissions",
65 npe.getMessage() );
66 }
67 }
68
69 public void testEntryCtor()
70 throws Exception
71 {
72 final URL url = new URL( "http://spice.sourveforge.net" );
73 final CodeSource codeSource = new CodeSource( url, new Certificate[ 0 ] );
74 final Permissions permissions = new Permissions();
75 try
76 {
77 final PolicyEntry entry = new PolicyEntry( codeSource, permissions );
78 assertEquals( "Entry.getCodeSource", codeSource, entry.getCodeSource() );
79 assertEquals( "Entry.getPermissions", permissions, entry.getPermissions() );
80 }
81 catch( final Throwable t )
82 {
83 fail( "Expected ctor not to except" );
84 }
85 }
86
87 public void testPolicyAccessPermission()
88 throws Exception
89 {
90 final URL url = new URL( "file:/-" );
91 final CodeSource codeSource = new CodeSource( url, new Certificate[ 0 ] );
92 final AllPermission allPermission = new AllPermission();
93 final HashMap grants = new HashMap();
94 grants.put( codeSource, new Permission[]{allPermission} );
95
96 final Policy policy = new DefaultPolicy( grants );
97 policy.refresh();
98 final PermissionCollection resultPermissions = policy.getPermissions( codeSource );
99 final Enumeration enumeration = resultPermissions.elements();
100 while( enumeration.hasMoreElements() )
101 {
102 final Permission permission = (Permission)enumeration.nextElement();
103 assertEquals( "Permissions for codeSource" + codeSource,
104 allPermission,
105 permission );
106 return;
107 }
108 fail( "Expected to find AllPermission in set" );
109 }
110
111 public void testPolicyAccessPermissionNotCovered()
112 throws Exception
113 {
114 final URL url = new URL( "http://spice.sourceforge.net/-" );
115 final CodeSource codeSource = new CodeSource( url, new Certificate[ 0 ] );
116 final AllPermission allPermission = new AllPermission();
117 final HashMap grants = new HashMap();
118 grants.put( codeSource, new Permission[]{allPermission} );
119
120 final Policy policy = new DefaultPolicy( grants );
121 policy.refresh();
122 final PermissionCollection resultPermissions = policy.getPermissions( new CodeSource( null, new Certificate[ 0 ] ) );
123 final Enumeration enumeration = resultPermissions.elements();
124 assertEquals( "Permissions for codeSource" + codeSource,
125 false, enumeration.hasMoreElements() );
126 }
127
128 public void testPolicyAccessPermissionForNonSpecifiedCodeBase()
129 throws Exception
130 {
131 final Policy policy = new DefaultPolicy();
132 policy.refresh();
133
134 final URL url = new URL( "http://spice.sourceforge.net/-" );
135 final CodeSource codeSource = new CodeSource( url, new Certificate[ 0 ] );
136 final PermissionCollection permissions = policy.getPermissions( codeSource );
137 assertEquals( "Expect no permissions for http://...", false, permissions.elements().hasMoreElements() );
138
139 final CodeSource otherCodeSource = new CodeSource( null, new Certificate[ 0 ] );
140 final PermissionCollection otherPermissions = policy.getPermissions( otherCodeSource );
141 assertEquals( "Expect no permissions for null location", false, otherPermissions.elements().hasMoreElements() );
142 }
143 }
This page was automatically generated by Maven