1 18 package org.apache.batik.util; 19 20 import org.apache.batik.test.*; 21 22 28 public class ApplicationSecurityEnforcerTest extends DefaultTestSuite { 29 final static Class APP_MAIN_CLASS = org.apache.batik.apps.svgbrowser.Main.class; 30 final static String APP_SECURITY_POLICY = "org/apache/batik/apps/svgbrowser/resources/svgbrowser.policy"; 31 32 35 public ApplicationSecurityEnforcerTest(){ 36 addTest(new CheckNoSecurityManagerOverride()); 37 addTest(new CheckSecurityEnforcement()); 38 addTest(new CheckSecurityRemoval()); 39 addTest(new CheckNoPolicyFile()); 40 } 41 42 static ApplicationSecurityEnforcer buildTestTarget(){ 43 return new ApplicationSecurityEnforcer(APP_MAIN_CLASS, 44 APP_SECURITY_POLICY); 45 } 46 47 static class CheckNoSecurityManagerOverride extends AbstractTest { 48 public boolean runImplBasic(){ 49 ApplicationSecurityEnforcer aseA 50 = buildTestTarget(); 51 52 aseA.enforceSecurity(true); 53 54 ApplicationSecurityEnforcer aseB 55 = buildTestTarget(); 56 57 boolean passed = false; 58 try { 59 aseB.enforceSecurity(true); 61 } catch (SecurityException se){ 62 System.out.println(">>>>>>>>>>>>> got expected SecurityException A"); 63 try { 64 System.out.println(">>>>>>>>>>>>> got expected SecurityException B"); 65 aseB.enforceSecurity(false); 66 } catch (SecurityException se2){ 67 passed = true; 68 } 69 } 70 71 aseA.enforceSecurity(false); 72 73 return passed; 74 } 75 } 76 77 static class CheckSecurityEnforcement extends AbstractTest { 78 public boolean runImplBasic() { 79 ApplicationSecurityEnforcer ase = buildTestTarget(); 80 81 try { 82 ase.enforceSecurity(true); 83 SecurityManager sm = System.getSecurityManager(); 84 if (sm == ase.lastSecurityManagerInstalled){ 85 return true; 86 } 87 } finally { 88 System.setSecurityManager(null); 89 } 90 91 return false; 92 } 93 } 94 95 static class CheckSecurityRemoval extends AbstractTest { 96 public boolean runImplBasic() { 97 ApplicationSecurityEnforcer ase = buildTestTarget(); 98 99 try { 100 ase.enforceSecurity(true); 101 ase.enforceSecurity(false); 102 SecurityManager sm = System.getSecurityManager(); 103 if (sm == null && ase.lastSecurityManagerInstalled == null) { 104 return true; 105 } 106 } finally { 107 System.setSecurityManager(null); 108 } 109 110 return false; 111 } 112 } 113 114 static class CheckNoPolicyFile extends AbstractTest { 115 public boolean runImplBasic() { 116 ApplicationSecurityEnforcer ase = 117 new ApplicationSecurityEnforcer(APP_MAIN_CLASS, 118 "dont.exist.policy"); 119 120 try { 121 ase.enforceSecurity(true); 122 } catch (NullPointerException se) { 123 return true; 124 } finally { 125 ase.enforceSecurity(false); 126 } 127 return false; 128 } 129 } 130 131 } 132 | Popular Tags |