1 17 18 package org.apache.tools.ant.types; 19 20 import junit.framework.TestCase; 21 22 import org.apache.tools.ant.ExitException; 23 24 28 public class PermissionsTest extends TestCase { 29 30 Permissions perms; 31 32 public PermissionsTest(String name) { 33 super(name); 34 } 35 36 public void setUp() { 37 perms = new Permissions(); 38 Permissions.Permission perm = new Permissions.Permission(); 39 perm.setActions("read, write"); 42 perm.setName("user.*"); 43 perm.setClass("java.util.PropertyPermission"); 44 perms.addConfiguredGrant(perm); 45 46 perm = new Permissions.Permission(); 47 perm.setActions("read"); 48 perm.setName("java.home"); 49 perm.setClass("java.util.PropertyPermission"); 50 perms.addConfiguredGrant(perm); 51 52 perm = new Permissions.Permission(); 53 perm.setActions("read"); 54 perm.setName("file.encoding"); 55 perm.setClass("java.util.PropertyPermission"); 56 perms.addConfiguredGrant(perm); 57 58 perm = new Permissions.Permission(); 61 perm.setActions("write"); 62 perm.setName("user.home"); 63 perm.setClass("java.util.PropertyPermission"); 64 perms.addConfiguredRevoke(perm); 65 66 perm = new Permissions.Permission(); 67 perm.setActions("read"); 68 perm.setName("os.*"); 69 perm.setClass("java.util.PropertyPermission"); 70 perms.addConfiguredRevoke(perm); 71 } 72 73 74 public void testDefaultGranted() { 75 perms.setSecurityManager(); 76 try { 77 String s = System.getProperty("line.separator"); 78 } finally { 79 perms.restoreSecurityManager(); 80 } 81 } 82 83 84 public void testGranted() { 85 perms.setSecurityManager(); 86 try { 87 String s = System.getProperty("user.name"); 88 System.setProperty("user.name", s); 89 } finally { 90 perms.restoreSecurityManager(); 91 } 92 } 93 94 95 public void testGrantedAndRevoked() { 96 perms.setSecurityManager(); 97 try { 98 String s = System.getProperty("user.home"); 99 System.setProperty("user.home", s); 100 fail("Could perform an action that should have been forbidden."); 101 } catch (SecurityException e){ 102 } finally { 104 perms.restoreSecurityManager(); 105 } 106 } 107 108 109 public void testDefaultRevoked() { 110 perms.setSecurityManager(); 111 try { 112 System.getProperty("os.name"); 113 fail("Could perform an action that should have been forbidden."); 114 } catch (SecurityException e){ 115 } finally { 117 perms.restoreSecurityManager(); 118 } 119 } 120 121 public void testOther() { 122 String ls = System.getProperty("line.separator"); 123 perms.setSecurityManager(); 124 try { 125 String s = System.setProperty("line.separator",ls); 126 fail("Could perform an action that should have been forbidden."); 127 } catch (SecurityException e){ 128 } finally { 130 perms.restoreSecurityManager(); 131 } 132 } 133 134 135 public void testExit() { 136 perms.setSecurityManager(); 137 try { 138 System.out.println("If this is the last line on standard out the testExit f.a.i.l.e.d"); 139 System.exit(3); 140 fail("Totaly impossible that this fail is ever executed. Please let me know if it is!"); 141 } catch (ExitException e) { 142 if (e.getStatus() != 3) { 143 fail("Received wrong exit status in Exit Exception."); 144 } 145 System.out.println("testExit successfull."); 146 } finally { 147 perms.restoreSecurityManager(); 148 } 149 } 150 151 152 public void tearDown() { 153 } 154 155 } 156 | Popular Tags |