1 /*
   2  * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package common;
  25 
  26 import java.io.InputStream;
  27 import java.io.StringWriter;
  28 import java.security.AllPermission;
  29 import java.security.Permission;
  30 import java.security.Permissions;
  31 
  32 import javax.xml.XMLConstants;
  33 import javax.xml.parsers.DocumentBuilder;
  34 import javax.xml.parsers.DocumentBuilderFactory;
  35 import javax.xml.transform.Transformer;
  36 import javax.xml.transform.TransformerFactory;
  37 import javax.xml.transform.dom.DOMSource;
  38 import javax.xml.transform.sax.SAXSource;
  39 import javax.xml.transform.stream.StreamResult;
  40 import javax.xml.transform.stream.StreamSource;
  41 import javax.xml.validation.Schema;
  42 import javax.xml.validation.SchemaFactory;
  43 import javax.xml.validation.Validator;
  44 import javax.xml.xpath.XPath;
  45 import javax.xml.xpath.XPathFactory;
  46 
  47 import jaxp.library.JAXPTestUtilities;
  48 
  49 import org.testng.Assert;
  50 import org.testng.annotations.Listeners;
  51 import org.testng.annotations.Test;
  52 import org.w3c.dom.Document;
  53 import org.xml.sax.InputSource;
  54 
  55 /*
  56  * @bug 6941169
  57  * @summary Test use-service-mechanism feature.
  58  */
  59 @Listeners({jaxp.library.FilePolicy.class})
  60 public class Bug6941169Test {
  61     static final String SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
  62     static final String SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
  63 
  64     private static final String DOM_FACTORY_ID = "javax.xml.parsers.DocumentBuilderFactory";
  65     private static final String SAX_FACTORY_ID = "javax.xml.parsers.SAXParserFactory";
  66 
  67     // impl specific feature
  68     final String ORACLE_FEATURE_SERVICE_MECHANISM = "http://www.oracle.com/feature/use-service-mechanism";
  69 
  70     static String _xml = Bug6941169Test.class.getResource("Bug6941169.xml").getPath();
  71     static String _xsd = Bug6941169Test.class.getResource("Bug6941169.xsd").getPath();
  72 
  73     @Test
  74     public void testValidation_SAX_withoutServiceMech() {
  75         System.out.println("Validation using SAX Source;  Service mechnism is turned off;  SAX Impl should be the default:");
  76         InputSource is = new InputSource(Bug6941169Test.class.getResourceAsStream("Bug6941169.xml"));
  77         SAXSource ss = new SAXSource(is);
  78         System.setProperty(SAX_FACTORY_ID, "MySAXFactoryImpl");
  79         long start = System.currentTimeMillis();
  80         try {
  81             SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
  82             factory.setFeature(ORACLE_FEATURE_SERVICE_MECHANISM, false);
  83             Schema schema = factory.newSchema(new StreamSource(_xsd));
  84             Validator validator = schema.newValidator();
  85             validator.validate(ss, null);
  86         } catch (Exception e) {
  87             // e.printStackTrace();
  88             String error = e.getMessage();
  89             if (error.indexOf("javax.xml.parsers.FactoryConfigurationError: Provider MySAXFactoryImpl not found") > 0) {
  90                 Assert.fail(e.getMessage());
  91             } else {
  92                 System.out.println("Default impl is used");
  93             }
  94 
  95             // System.out.println(e.getMessage());
  96 
  97         }
  98         long end = System.currentTimeMillis();
  99         double elapsedTime = ((end - start));
 100         System.out.println("Time elapsed: " + elapsedTime);
 101         System.clearProperty(SAX_FACTORY_ID);
 102     }
 103 
 104     @Test
 105     public void testValidation_SAX_withServiceMech() {
 106         System.out.println("Validation using SAX Source. Using service mechnism (by default) to find SAX Impl:");
 107         InputSource is = new InputSource(Bug6941169Test.class.getResourceAsStream("Bug6941169.xml"));
 108         SAXSource ss = new SAXSource(is);
 109         System.setProperty(SAX_FACTORY_ID, "MySAXFactoryImpl");
 110         long start = System.currentTimeMillis();
 111         try {
 112             SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
 113             Schema schema = factory.newSchema(new StreamSource(_xsd));
 114             Validator validator = schema.newValidator();
 115             validator.validate(ss, null);
 116             Assert.fail("User impl MySAXFactoryImpl should be used.");
 117         } catch (Exception e) {
 118             String error = e.getMessage();
 119             if (error.indexOf("javax.xml.parsers.FactoryConfigurationError: Provider MySAXFactoryImpl not found") > 0) {
 120                 // expected
 121             }
 122             // System.out.println(e.getMessage());
 123 
 124         }
 125         long end = System.currentTimeMillis();
 126         double elapsedTime = ((end - start));
 127         System.out.println("Time elapsed: " + elapsedTime);
 128         System.clearProperty(SAX_FACTORY_ID);
 129     }
 130 
 131     @Test
 132     public void testValidation_SAX_withSM() {
 133         System.out.println("Validation using SAX Source with security manager:");
 134         InputSource is = new InputSource(Bug6941169Test.class.getResourceAsStream("Bug6941169.xml"));
 135         SAXSource ss = new SAXSource(is);
 136         System.setProperty(SAX_FACTORY_ID, "MySAXFactoryImpl");
 137         Permissions granted = new java.security.Permissions();
 138         granted.add(new AllPermission());
 139         System.setSecurityManager(new MySM(granted));
 140 
 141         long start = System.currentTimeMillis();
 142         try {
 143             SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
 144             factory.setFeature(ORACLE_FEATURE_SERVICE_MECHANISM, false);
 145             Schema schema = factory.newSchema(new StreamSource(_xsd));
 146             Validator validator = schema.newValidator();
 147             validator.validate(ss, null);
 148         } catch (Exception e) {
 149             String error = e.getMessage();
 150             if (error.indexOf("javax.xml.parsers.FactoryConfigurationError: Provider MySAXFactoryImpl not found") > 0) {
 151                 Assert.fail(e.getMessage());
 152             } else {
 153                 System.out.println("Default impl is used");
 154             }
 155 
 156             // System.out.println(e.getMessage());
 157 
 158         } finally {
 159             System.clearProperty(SAX_FACTORY_ID);
 160             System.setSecurityManager(null);
 161         }
 162         long end = System.currentTimeMillis();
 163         double elapsedTime = ((end - start));
 164         System.out.println("Time elapsed: " + elapsedTime);
 165         System.setSecurityManager(null);
 166 
 167     }
 168 
 169     @Test
 170     public void testTransform_DOM_withoutServiceMech() {
 171         System.out.println("Transform using DOM Source;  Service mechnism is turned off;  Default DOM Impl should be the default:");
 172         DOMSource domSource = new DOMSource();
 173         domSource.setSystemId(_xml);
 174 
 175         // DOMSource domSource = new
 176         // DOMSource(getDocument(Bug6941169Test.class.getResourceAsStream("Bug6941169.xml")));
 177         System.setProperty(DOM_FACTORY_ID, "MyDOMFactoryImpl");
 178         long start = System.currentTimeMillis();
 179         try {
 180             TransformerFactory factory = TransformerFactory.newInstance();
 181             factory.setFeature(ORACLE_FEATURE_SERVICE_MECHANISM, false);
 182 
 183             Transformer t = factory.newTransformer();
 184 
 185             StringWriter result = new StringWriter();
 186             StreamResult streamResult = new StreamResult(result);
 187             t.transform(domSource, streamResult);
 188             System.out.println("Writing to " + result.toString());
 189 
 190         } catch (Exception e) {
 191             // e.printStackTrace();
 192             String error = e.getMessage();
 193             if (error.indexOf("Provider MyDOMFactoryImpl not found") > 0) {
 194                 Assert.fail(e.getMessage());
 195             } else {
 196                 System.out.println("Default impl is used");
 197             }
 198 
 199             // System.out.println(e.getMessage());
 200 
 201         } catch (Error e) {
 202             // e.printStackTrace();
 203             String error = e.getMessage();
 204             if (error.indexOf("Provider MyDOMFactoryImpl not found") > 0) {
 205                 Assert.fail(e.getMessage());
 206             } else {
 207                 System.out.println("Default impl is used");
 208             }
 209 
 210             // System.out.println(e.getMessage());
 211 
 212         }
 213 
 214         long end = System.currentTimeMillis();
 215         double elapsedTime = ((end - start));
 216         System.out.println("Time elapsed: " + elapsedTime);
 217         System.clearProperty(DOM_FACTORY_ID);
 218     }
 219 
 220     /** this is by default */
 221     @Test
 222     public void testTransform_DOM_withServiceMech() {
 223         System.out.println("Transform using DOM Source;  By default, the factory uses services mechanism to look up impl:");
 224         DOMSource domSource = new DOMSource();
 225         domSource.setSystemId(_xml);
 226 
 227         // DOMSource domSource = new
 228         // DOMSource(getDocument(Bug6941169Test.class.getResourceAsStream("Bug6941169.xml")));
 229         System.setProperty(DOM_FACTORY_ID, "MyDOMFactoryImpl");
 230         long start = System.currentTimeMillis();
 231         try {
 232             TransformerFactory factory = TransformerFactory.newInstance();
 233             Transformer t = factory.newTransformer();
 234 
 235             StringWriter result = new StringWriter();
 236             StreamResult streamResult = new StreamResult(result);
 237             t.transform(domSource, streamResult);
 238             System.out.println("Writing to " + result.toString());
 239 
 240             Assert.fail("User impl MyDOMFactoryImpl should be used.");
 241 
 242         } catch (Exception e) {
 243             String error = e.getMessage();
 244             if (error.indexOf("Provider MyDOMFactoryImpl not found") > 0) {
 245                 // expected
 246             }
 247             System.out.println(error);
 248 
 249         } catch (Error e) {
 250             String error = e.getMessage();
 251             if (error.indexOf("Provider MyDOMFactoryImpl not found") > 0) {
 252                 // expected
 253             }
 254             System.out.println(error);
 255 
 256         }
 257 
 258         long end = System.currentTimeMillis();
 259         double elapsedTime = ((end - start));
 260         System.out.println("Time elapsed: " + elapsedTime);
 261         System.clearProperty(DOM_FACTORY_ID);
 262     }
 263 
 264     @Test
 265     public void testTransform_DOM_withSM() {
 266         System.out.println("Transform using DOM Source;  Security Manager is set:");
 267         DOMSource domSource = new DOMSource();
 268         domSource.setSystemId(_xml);
 269 
 270         // DOMSource domSource = new
 271         // DOMSource(getDocument(Bug6941169Test.class.getResourceAsStream("Bug6941169.xml")));
 272         Permissions granted = new java.security.Permissions();
 273         granted.add(new AllPermission());
 274         System.setSecurityManager(new MySM(granted));
 275         System.setProperty(DOM_FACTORY_ID, "MyDOMFactoryImpl");
 276         long start = System.currentTimeMillis();
 277         try {
 278             TransformerFactory factory = TransformerFactory.newInstance("com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl",
 279                     TransformerFactory.class.getClassLoader());
 280             Transformer t = factory.newTransformer();
 281 
 282             StringWriter result = new StringWriter();
 283             StreamResult streamResult = new StreamResult(result);
 284             t.transform(domSource, streamResult);
 285             System.out.println("Writing to " + result.toString());
 286 
 287         } catch (Exception e) {
 288             String error = e.getMessage();
 289             if (error.indexOf("Provider MyDOMFactoryImpl not found") > 0) {
 290                 Assert.fail(e.getMessage());
 291             } else {
 292                 System.out.println("Default impl is used");
 293             }
 294 
 295             // System.out.println(e.getMessage());
 296 
 297         } catch (Error e) {
 298             String error = e.getMessage();
 299             if (error.indexOf("Provider MyDOMFactoryImpl not found") > 0) {
 300                 Assert.fail(e.getMessage());
 301             } else {
 302                 System.out.println("Default impl is used");
 303             }
 304 
 305             // System.out.println(e.getMessage());
 306 
 307         } finally {
 308             System.clearProperty(DOM_FACTORY_ID);
 309             System.setSecurityManager(null);
 310         }
 311         long end = System.currentTimeMillis();
 312         double elapsedTime = ((end - start));
 313         System.out.println("Time elapsed: " + elapsedTime);
 314         System.clearProperty(DOM_FACTORY_ID);
 315     }
 316 
 317     @Test
 318     public void testXPath_DOM_withoutServiceMech() {
 319         final String XPATH_EXPRESSION = "/fooTest";
 320         System.out.println("Evaluate DOM Source;  Service mechnism is turned off;  Default DOM Impl should be used:");
 321         Document doc = getDocument(Bug6941169Test.class.getResourceAsStream("Bug6941169.xml"));
 322         System.setProperty(DOM_FACTORY_ID, "MyDOMFactoryImpl");
 323         long start = System.currentTimeMillis();
 324         try {
 325             XPathFactory xPathFactory = XPathFactory.newInstance();
 326             xPathFactory.setFeature(ORACLE_FEATURE_SERVICE_MECHANISM, false);
 327 
 328             XPath xPath = xPathFactory.newXPath();
 329 
 330             String xPathResult = xPath.evaluate(XPATH_EXPRESSION, doc);
 331 
 332         } catch (Exception e) {
 333             // e.printStackTrace();
 334             String error = e.getMessage();
 335             if (error.indexOf("MyDOMFactoryImpl not found") > 0) {
 336                 Assert.fail(e.getMessage());
 337             } else {
 338                 System.out.println("Default impl is used");
 339             }
 340 
 341             // System.out.println(e.getMessage());
 342 
 343         } catch (Error e) {
 344             // e.printStackTrace();
 345             String error = e.getMessage();
 346             if (error.indexOf("MyDOMFactoryImpl not found") > 0) {
 347                 Assert.fail(e.getMessage());
 348             } else {
 349                 System.out.println("Default impl is used");
 350             }
 351 
 352             // System.out.println(e.getMessage());
 353 
 354         }
 355 
 356         long end = System.currentTimeMillis();
 357         double elapsedTime = ((end - start));
 358         System.out.println("Time elapsed: " + elapsedTime);
 359         System.clearProperty(DOM_FACTORY_ID);
 360     }
 361 
 362     @Test
 363     public void testXPath_DOM_withServiceMech() {
 364         final String XPATH_EXPRESSION = "/fooTest";
 365         System.out.println("Evaluate DOM Source;  Service mechnism is on by default;  It would try to use MyDOMFactoryImpl:");
 366         InputStream input = getClass().getResourceAsStream("Bug6941169.xml");
 367         InputSource source = new InputSource(input);
 368         System.setProperty(DOM_FACTORY_ID, "MyDOMFactoryImpl");
 369         long start = System.currentTimeMillis();
 370         try {
 371             XPathFactory xPathFactory = XPathFactory.newInstance();
 372 
 373             XPath xPath = xPathFactory.newXPath();
 374 
 375             String xPathResult = xPath.evaluate(XPATH_EXPRESSION, source);
 376             Assert.fail("User impl MyDOMFactoryImpl should be used.");
 377 
 378         } catch (Exception e) {
 379             // e.printStackTrace();
 380             String error = e.getMessage();
 381             if (error.indexOf("MyDOMFactoryImpl not found") > 0) {
 382                 System.out.println("Tried to locate MyDOMFactoryImpl");
 383             } else {
 384                 Assert.fail(e.getMessage());
 385 
 386             }
 387 
 388             // System.out.println(e.getMessage());
 389 
 390         } catch (Error e) {
 391             // e.printStackTrace();
 392             String error = e.getMessage();
 393             if (error.indexOf("MyDOMFactoryImpl not found") > 0) {
 394                 System.out.println("Tried to locate MyDOMFactoryImpl");
 395             } else {
 396                 Assert.fail(e.getMessage());
 397 
 398             }
 399 
 400             // System.out.println(e.getMessage());
 401 
 402         }
 403 
 404         long end = System.currentTimeMillis();
 405         double elapsedTime = ((end - start));
 406         System.out.println("Time elapsed: " + elapsedTime);
 407         System.clearProperty(DOM_FACTORY_ID);
 408     }
 409 
 410     @Test
 411     public void testXPath_DOM_withSM() {
 412         final String XPATH_EXPRESSION = "/fooTest";
 413         System.out.println("Evaluate DOM Source;  Security Manager is set:");
 414         Permissions granted = new java.security.Permissions();
 415         granted.add(new AllPermission());
 416         System.setSecurityManager(new MySM(granted));
 417         InputStream input = getClass().getResourceAsStream("Bug6941169.xml");
 418         InputSource source = new InputSource(input);
 419         System.setProperty(DOM_FACTORY_ID, "MyDOMFactoryImpl");
 420         long start = System.currentTimeMillis();
 421         try {
 422             XPathFactory xPathFactory = XPathFactory.newInstance("http://java.sun.com/jaxp/xpath/dom",
 423                     "com.sun.org.apache.xpath.internal.jaxp.XPathFactoryImpl", null);
 424 
 425             XPath xPath = xPathFactory.newXPath();
 426 
 427             String xPathResult = xPath.evaluate(XPATH_EXPRESSION, source);
 428             System.out.println("Use default impl");
 429         } catch (Exception e) {
 430             // e.printStackTrace();
 431             String error = e.getMessage();
 432             if (error.indexOf("MyDOMFactoryImpl not found") > 0) {
 433                 Assert.fail(e.getMessage());
 434             } else {
 435                 System.out.println("Default impl should be used");
 436             }
 437 
 438             // System.out.println(e.getMessage());
 439 
 440         } catch (Error e) {
 441             // e.printStackTrace();
 442             String error = e.getMessage();
 443             if (error.indexOf("MyDOMFactoryImpl not found") > 0) {
 444                 Assert.fail(e.getMessage());
 445             } else {
 446                 System.out.println("Default impl should be used");
 447             }
 448 
 449             // System.out.println(e.getMessage());
 450 
 451         } finally {
 452             System.clearProperty(DOM_FACTORY_ID);
 453             System.setSecurityManager(null);
 454         }
 455         long end = System.currentTimeMillis();
 456         double elapsedTime = ((end - start));
 457         System.out.println("Time elapsed: " + elapsedTime);
 458         System.clearProperty(DOM_FACTORY_ID);
 459     }
 460 
 461     @Test
 462     public void testSM() {
 463         SecurityManager sm = System.getSecurityManager();
 464         if (System.getSecurityManager() != null) {
 465             System.out.println("Security manager not cleared: " + sm.toString());
 466         } else {
 467             System.out.println("Security manager cleared: ");
 468         }
 469     }
 470 
 471     private static Document getDocument(InputStream in) {
 472 
 473         Document document = null;
 474 
 475         try {
 476             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 477             dbf.setNamespaceAware(true);
 478             DocumentBuilder db = dbf.newDocumentBuilder();
 479             document = db.parse(in);
 480         } catch (Exception e) {
 481             e.printStackTrace();
 482             Assert.fail(e.toString());
 483         }
 484 
 485         return document;
 486     }
 487 
 488     class MySM extends SecurityManager {
 489         Permissions granted;
 490 
 491         public MySM(Permissions perms) {
 492             granted = perms;
 493         }
 494 
 495         @Override
 496         public void checkPermission(Permission perm) {
 497             if (granted.implies(perm)) {
 498                 return;
 499             }
 500             super.checkPermission(perm);
 501         }
 502 
 503     }
 504 
 505 }