1 18 package org.apache.batik.test.svg; 19 20 import java.io.File ; 21 import java.net.MalformedURLException ; 22 import java.net.URL ; 23 24 import org.w3c.dom.Document ; 25 import org.w3c.dom.Element ; 26 27 import org.apache.batik.bridge.BaseScriptingEnvironment; 28 import org.apache.batik.bridge.BridgeContext; 29 import org.apache.batik.bridge.BridgeException; 30 import org.apache.batik.bridge.DefaultExternalResourceSecurity; 31 import org.apache.batik.bridge.DefaultScriptSecurity; 32 import org.apache.batik.bridge.EmbededExternalResourceSecurity; 33 import org.apache.batik.bridge.EmbededScriptSecurity; 34 import org.apache.batik.bridge.ExternalResourceSecurity; 35 import org.apache.batik.bridge.GVTBuilder; 36 import org.apache.batik.bridge.NoLoadExternalResourceSecurity; 37 import org.apache.batik.bridge.NoLoadScriptSecurity; 38 import org.apache.batik.bridge.RelaxedExternalResourceSecurity; 39 import org.apache.batik.bridge.RelaxedScriptSecurity; 40 import org.apache.batik.bridge.ScriptSecurity; 41 import org.apache.batik.bridge.UserAgent; 42 import org.apache.batik.bridge.UserAgentAdapter; 43 import org.apache.batik.dom.svg.SAXSVGDocumentFactory; 44 import org.apache.batik.test.AbstractTest; 45 import org.apache.batik.test.TestReport; 46 import org.apache.batik.util.ParsedURL; 47 import org.apache.batik.util.XMLResourceDescriptor; 48 import org.apache.batik.util.ApplicationSecurityEnforcer; 49 50 import java.security.AccessController ; 51 import java.security.AccessControlContext ; 52 import java.security.CodeSource ; 53 import java.security.PrivilegedExceptionAction ; 54 import java.security.PrivilegedActionException ; 55 import java.security.ProtectionDomain ; 56 import java.security.Permission ; 57 import java.security.PermissionCollection ; 58 import java.security.Permissions ; 59 import java.security.Policy ; 60 61 import java.io.FilePermission ; 62 63 import java.util.Enumeration ; 64 65 89 public class SVGOnLoadExceptionTest extends AbstractTest { 90 93 public static final String RAN = "ran"; 94 95 98 public static final String ERROR_EXCEPTION_DID_NOT_OCCUR 99 = "SVGOnLoadExceptionTest.error.exception.did.not.occur"; 100 101 105 public static final String ERROR_UNEXPECTED_EXCEPTION 106 = "SVGOnLoadExceptionTest.error.unexpected.exception"; 107 108 112 public static final String ERROR_UNEXPECTED_ERROR_CODE 113 = "SVGOnLoadExceptionTest.error.unexpected.error.code"; 114 115 118 public static final String ERROR_SCRIPT_DID_NOT_RUN 119 = "SVGOnLoadExceptionTest.error.script.did.not.run"; 120 121 124 public static final String ENTRY_KEY_UNEXPECTED_EXCEPTION 125 = "SVGOnLoadExceptionTest.entry.key.unexpected.exception"; 126 127 130 public static final String ENTRY_KEY_UNEXPECTED_ERROR_CODE 131 = "SVGOnLoadExceptionTest.entry.key.unexpected.error.code"; 132 133 136 public static final String ENTRY_KEY_EXPECTED_ERROR_CODE 137 = "SVGOnLoadExceptionTest.entry.key.expected.error.code"; 138 139 142 public static final String ENTRY_KEY_EXPECTED_EXCEPTION 143 = "SVGOnLoadExceptionTest.entry.key.expected.exception"; 144 145 148 public static final String ENTRY_KEY_UNEXPECTED_RESULT 149 = "SVGOnLoadExceptionTest.entry.key.unexpected.result"; 150 151 154 public static final String ERROR_CODE_NO_CHECK 155 = "noCheck"; 156 157 160 public static final String testNS = "http://xml.apache.org/batik/test"; 161 162 165 protected String svgURL; 166 167 170 protected String scripts = "text/ecmascript, application/java-archive"; 171 172 175 protected String expectedExceptionClass = "org.apache.batik.bridge.Exception"; 176 177 180 protected String expectedErrorCode = "none"; 181 182 185 protected String scriptOrigin = "ANY"; 186 187 190 protected String resourceOrigin = "ANY"; 191 192 195 protected boolean secure = false; 196 197 200 protected Boolean validate = new Boolean (false); 201 202 205 protected String fileName; 206 207 211 protected boolean restricted = false; 212 213 public boolean getRestricted() { 214 return restricted; 215 } 216 217 public void setRestricted(boolean restricted) { 218 this.restricted = restricted; 219 } 220 221 public void setScripts(String scripts){ 222 this.scripts = scripts; 223 } 224 225 public String getScripts(){ 226 return scripts; 227 } 228 229 public void setScriptOrigin(String scriptOrigin){ 230 this.scriptOrigin = scriptOrigin; 231 } 232 233 public String getScriptOrigin(){ 234 return this.scriptOrigin; 235 } 236 237 public void setResourceOrigin(String resourceOrigin){ 238 this.resourceOrigin = resourceOrigin; 239 } 240 241 public String getResourceOrigin(){ 242 return this.resourceOrigin; 243 } 244 245 public void setSecure(boolean secure){ 246 this.secure = secure; 247 } 248 249 public boolean getSecure(){ 250 return secure; 251 } 252 253 public void setExpectedExceptionClass(String expectedExceptionClass){ 254 this.expectedExceptionClass = expectedExceptionClass; 255 } 256 257 public String getExpectedExceptionClass(){ 258 return this.expectedExceptionClass; 259 } 260 261 public void setExpectedErrorCode(String expectedErrorCode){ 262 this.expectedErrorCode = expectedErrorCode; 263 } 264 265 public String getExpectedErrorCode(){ 266 return this.expectedErrorCode; 267 } 268 269 public Boolean getValidate() { 270 return validate; 271 } 272 273 public void setValidate(Boolean validate) { 274 this.validate = validate; 275 if (this.validate == null) { 276 this.validate = new Boolean (false); 277 } 278 } 279 280 283 public SVGOnLoadExceptionTest(){ 284 } 285 286 public void setId(String id){ 287 super.setId(id); 288 289 if (id != null) { 290 int i = id.indexOf("("); 291 if (i != -1) { 292 id = id.substring(0, i); 293 } 294 fileName = "test-resources/org/apache/batik/" + id + ".svg"; 295 svgURL = resolveURL(fileName); 296 } 297 } 298 299 307 protected String resolveURL(String url){ 308 File f = (new File (url)).getAbsoluteFile(); 310 if(f.getParentFile().exists()){ 311 try{ 312 return f.toURL().toString(); 313 }catch(MalformedURLException e){ 314 throw new IllegalArgumentException (); 315 } 316 } 317 318 try{ 320 return (new URL (url)).toString(); 321 }catch(MalformedURLException e){ 322 throw new IllegalArgumentException (url); 323 } 324 } 325 326 327 336 public TestReport runImpl() throws Exception { 337 ApplicationSecurityEnforcer ase 338 = new ApplicationSecurityEnforcer(this.getClass(), 339 "org/apache/batik/apps/svgbrowser/resources/svgbrowser.policy"); 340 341 if (secure) { 342 ase.enforceSecurity(true); 343 } 344 345 try { 346 if (!restricted) { 347 return testImpl(); 348 } else { 349 Policy policy = Policy.getPolicy(); 353 URL classesURL = (new File ("classes")).toURL(); 354 CodeSource cs = new CodeSource (classesURL, null); 355 PermissionCollection permissionsOrig 356 = policy.getPermissions(cs); 357 Permissions permissions = new Permissions (); 358 Enumeration iter = permissionsOrig.elements(); 359 while (iter.hasMoreElements()) { 360 Permission p = (Permission )iter.nextElement(); 361 if (!(p instanceof RuntimePermission )) { 362 if (!(p instanceof java.security.AllPermission )) { 363 permissions.add(p); 364 } 365 } else { 366 if (!"createClassLoader".equals(p.getName())) { 367 permissions.add(p); 368 } 369 } 370 } 371 372 permissions.add(new FilePermission (fileName, "read")); 373 permissions.add(new RuntimePermission ("accessDeclaredMembers")); 374 375 ProtectionDomain domain; 376 AccessControlContext ctx; 377 domain = new ProtectionDomain (null, permissions); 378 ctx = new AccessControlContext (new ProtectionDomain []{domain}); 379 380 try { 381 return (TestReport)AccessController.doPrivileged 382 (new PrivilegedExceptionAction () { 383 public Object run() throws Exception { 384 return testImpl(); 385 } 386 }, ctx); 387 } catch (PrivilegedActionException pae) { 388 throw pae.getException(); 389 } 390 } 391 } finally { 392 ase.enforceSecurity(false); 393 } 394 } 395 396 399 protected TestReport testImpl() { 400 String parserClassName = XMLResourceDescriptor.getXMLParserClassName(); 406 SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parserClassName); 407 f.setValidating(validate.booleanValue()); 408 Document doc = null; 409 410 try { 411 doc = f.createDocument(svgURL); 412 } catch(Exception e){ 413 e.printStackTrace(); 414 return handleException(e); 415 } 416 417 TestUserAgent userAgent = buildUserAgent(); 424 GVTBuilder builder = new GVTBuilder(); 425 BridgeContext ctx = new BridgeContext(userAgent); 426 ctx.setDynamic(true); 427 Exception e = null; 428 try { 429 builder.build(ctx, doc); 430 BaseScriptingEnvironment scriptEnvironment 431 = new BaseScriptingEnvironment(ctx); 432 scriptEnvironment.loadScripts(); 433 scriptEnvironment.dispatchSVGLoadEvent(); 434 } catch (Exception ex){ 435 e = ex; 436 } finally { 437 if (e == null && userAgent.e != null) { 438 e = userAgent.e; 439 } 440 441 if (e != null) { 442 return handleException(e); 443 } 444 } 445 446 TestReport report = null; 450 if (expectedExceptionClass == null) { 451 Element elem = doc.getElementById("testResult"); 453 String s = elem.getAttributeNS(null, "result"); 454 if (RAN.equals(s)) { 455 report = reportSuccess(); 456 } else { 457 report = reportError(ERROR_SCRIPT_DID_NOT_RUN); 458 report.addDescriptionEntry(ENTRY_KEY_UNEXPECTED_RESULT, 459 s); 460 } 461 } 462 if (report == null) { 463 report = reportError(ERROR_EXCEPTION_DID_NOT_OCCUR); 464 report.addDescriptionEntry(ENTRY_KEY_EXPECTED_EXCEPTION, 465 expectedExceptionClass); 466 } 467 return report; 468 } 469 470 474 protected TestReport handleException(Exception e) { 475 if (!isMatch(e.getClass(), expectedExceptionClass)) { 476 TestReport report = reportError(ERROR_UNEXPECTED_EXCEPTION); 477 report.addDescriptionEntry(ENTRY_KEY_UNEXPECTED_EXCEPTION, 478 e.getClass().getName()); 479 report.addDescriptionEntry(ENTRY_KEY_EXPECTED_EXCEPTION, 480 expectedExceptionClass); 481 return report; 482 } else { 483 if (!ERROR_CODE_NO_CHECK.equals(expectedErrorCode) 484 && e instanceof BridgeException) { 485 if ( !expectedErrorCode.equals(((BridgeException)e).getCode()) ) { 486 TestReport report = reportError(ERROR_UNEXPECTED_ERROR_CODE); 487 report.addDescriptionEntry(ENTRY_KEY_UNEXPECTED_ERROR_CODE, 488 ((BridgeException)e).getCode()); 489 report.addDescriptionEntry(ENTRY_KEY_EXPECTED_ERROR_CODE, 490 expectedErrorCode); 491 return report; 492 } 493 } 494 return reportSuccess(); 495 } 496 } 497 498 502 protected boolean isMatch(final Class cl, final String name) { 503 if (cl == null) { 504 return false; 505 } else if (cl.getName().equals(name)) { 506 return true; 507 } else { 508 return isMatch(cl.getSuperclass(), name); 509 } 510 } 511 512 515 protected TestUserAgent buildUserAgent(){ 516 return new TestUserAgent(); 517 } 518 519 class TestUserAgent extends UserAgentAdapter { 520 Exception e; 521 522 public ExternalResourceSecurity 523 getExternalResourceSecurity(ParsedURL resourceURL, 524 ParsedURL docURL) { 525 if ("ANY".equals(resourceOrigin)) { 526 return new RelaxedExternalResourceSecurity(resourceURL, 527 docURL); 528 } else if ("DOCUMENT".equals(resourceOrigin)) { 529 return new DefaultExternalResourceSecurity(resourceURL, 530 docURL); 531 } else if ("EMBEDED".equals(resourceOrigin)) { 532 return new EmbededExternalResourceSecurity(resourceURL); 533 } else { 534 return new NoLoadExternalResourceSecurity(); 535 } 536 } 537 538 public ScriptSecurity 539 getScriptSecurity(String scriptType, 540 ParsedURL scriptURL, 541 ParsedURL docURL) { 542 ScriptSecurity result = null; 543 if (scripts.indexOf(scriptType) == -1) { 544 result = new NoLoadScriptSecurity(scriptType); 545 } else { 546 if ("ANY".equals(scriptOrigin)) { 547 result = new RelaxedScriptSecurity(scriptType, 548 scriptURL, 549 docURL); 550 } else if ("DOCUMENT".equals(scriptOrigin)) { 551 result = new DefaultScriptSecurity(scriptType, 552 scriptURL, 553 docURL); 554 } else if ("EMBEDED".equals(scriptOrigin)) { 555 result = new EmbededScriptSecurity(scriptType, 556 scriptURL, 557 docURL); 558 } else { 559 result = new NoLoadScriptSecurity(scriptType); 560 } 561 } 562 return result; 563 } 564 565 public void displayError(Exception e) { 566 this.e = e; 567 } 568 } 569 570 } 571 | Popular Tags |