1 16 17 package org.apache.batik.bridge; 18 19 import org.apache.batik.test.*; 20 import org.apache.batik.util.ApplicationSecurityEnforcer; 21 import org.apache.batik.util.ParsedURL; 22 import org.apache.batik.test.svg.SelfContainedSVGOnLoadTest; 23 24 import java.security.AccessController ; 25 import java.security.AccessControlContext ; 26 import java.security.CodeSource ; 27 import java.security.PrivilegedExceptionAction ; 28 import java.security.PrivilegedActionException ; 29 import java.security.ProtectionDomain ; 30 import java.security.Permission ; 31 import java.security.PermissionCollection ; 32 import java.security.Permissions ; 33 import java.security.Policy ; 34 35 import java.io.FilePermission ; 36 import java.io.File ; 37 38 import java.net.URL ; 39 40 import java.util.Enumeration ; 41 48 49 public class ScriptSelfTest extends SelfContainedSVGOnLoadTest { 50 String scripts = "text/ecmascript, application/java-archive"; 51 boolean secure = true; 52 String scriptOrigin = "any"; 53 String fileName; 54 55 TestUserAgent userAgent = new TestUserAgent(); 56 57 public void setId(String id){ 58 super.setId(id); 59 60 if (id != null) { 61 int i = id.indexOf("("); 62 if (i != -1) { 63 id = id.substring(0, i); 64 } 65 fileName = "test-resources/org/apache/batik/bridge/" + id + ".svg"; 66 svgURL = resolveURL(fileName); 67 } 68 } 69 70 public void setSecure(boolean secure){ 71 this.secure = secure; 72 } 73 74 public boolean getSecure(){ 75 return secure; 76 } 77 78 public String getScriptOrigin() { 79 return scriptOrigin; 80 } 81 82 public void setScriptOrigin(String scriptOrigin) { 83 this.scriptOrigin = scriptOrigin; 84 } 85 86 public void setScripts(String scripts){ 87 this.scripts = scripts; 88 } 89 90 public String getScripts(){ 91 return scripts; 92 } 93 94 public TestReport runImpl() throws Exception { 95 ApplicationSecurityEnforcer ase 96 = new ApplicationSecurityEnforcer(this.getClass(), 97 "org/apache/batik/apps/svgbrowser/resources/svgbrowser.policy"); 98 99 if (secure) { 100 ase.enforceSecurity(true); 101 } 102 103 try { 104 return super.runImpl(); 105 } catch (ExceptionInInitializerError e) { 106 e.printStackTrace(); 107 throw e; 108 } catch (NoClassDefFoundError e) { 109 throw new Exception (e.getMessage()); 111 } finally { 112 ase.enforceSecurity(false); 113 } 114 } 115 116 protected UserAgent buildUserAgent(){ 117 return userAgent; 118 } 119 120 class TestUserAgent extends UserAgentAdapter { 121 public ScriptSecurity getScriptSecurity(String scriptType, 122 ParsedURL scriptPURL, 123 ParsedURL docPURL){ 124 ScriptSecurity scriptSecurity = null; 125 if (scripts.indexOf(scriptType) == -1){ 126 scriptSecurity = new NoLoadScriptSecurity(scriptType); 127 } else { 128 if ("any".equals(scriptOrigin)) { 129 scriptSecurity = new RelaxedScriptSecurity 130 (scriptType, scriptPURL, docPURL); 131 } else if ("document".equals(scriptOrigin)) { 132 scriptSecurity = new DefaultScriptSecurity 133 (scriptType, scriptPURL, docPURL); 134 } else if ("embeded".equals(scriptOrigin)) { 135 scriptSecurity = new EmbededScriptSecurity 136 (scriptType, scriptPURL, docPURL); 137 } else if ("none".equals(scriptOrigin)) { 138 scriptSecurity = new NoLoadScriptSecurity(scriptType); 139 } else { 140 throw new Error ("Wrong scriptOrigin : " + scriptOrigin); 141 } 142 } 143 144 return scriptSecurity; 145 } 146 } 147 148 } 149 | Popular Tags |