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 dom;
  25 
  26 import java.io.ByteArrayInputStream;
  27 import java.io.IOException;
  28 import java.io.InputStreamReader;
  29 
  30 import javax.xml.parsers.DocumentBuilder;
  31 import javax.xml.parsers.DocumentBuilderFactory;
  32 import javax.xml.parsers.ParserConfigurationException;
  33 
  34 import org.testng.Assert;
  35 import org.testng.annotations.Listeners;
  36 import org.testng.annotations.Test;
  37 import org.w3c.dom.DOMException;
  38 import org.w3c.dom.Document;
  39 import org.w3c.dom.Entity;
  40 import org.xml.sax.InputSource;
  41 import org.xml.sax.SAXException;
  42 
  43 /*
  44  * @test
  45  * @bug 6517707
  46  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  47  * @run testng/othervm -DrunSecMngr=true dom.CR6517707Test
  48  * @run testng/othervm dom.CR6517707Test
  49  * @summary Test Node.setNodeValue(value) shall throw DOMException.NO_MODIFICATION_ALLOWED_ERR if the node is read-only.
  50  */
  51 @Listeners({jaxp.library.BasePolicy.class})
  52 public class CR6517707Test {
  53 
  54     @Test
  55     public void testCanonicalForm001() {
  56         String data = "<?xml version=\"1.0\" ?>" + "<!DOCTYPE root [" + "<!ELEMENT root ANY>" + "<!ENTITY ent \"foo\">"
  57                 + "<!NOTATION not PUBLIC \"http://xxx.xxx.xx/x.txt\">" + "]>" + "<root>" + "</root>";
  58 
  59         Document document = null;
  60         try {
  61             DocumentBuilderFactory docBF = DocumentBuilderFactory.newInstance();
  62             docBF.setNamespaceAware(true);
  63             DocBuilderWrapper docBuilder = new DocBuilderWrapper(docBF.newDocumentBuilder());
  64             document = docBuilder.parse(data);
  65         } catch (ParserConfigurationException e) {
  66             // return Status.failed(e.toString());
  67         } catch (IOException e) {
  68             // return Status.failed(e.toString());
  69         } catch (SAXException e) {
  70             // return Status.failed(e.toString());
  71         }
  72 
  73         Entity anEntity = (Entity) document.getDoctype().getEntities().item(0);
  74         boolean success = false;
  75         try {
  76             anEntity.setNodeValue("someValue"); // on jdk 6, not even throwing
  77                                                 // exception
  78 
  79             System.out.println("Should throw DOMException: NO_MODIFICATION_ALLOWED_ERR ");
  80         } catch (DOMException e) {
  81             if (e.code == DOMException.NO_MODIFICATION_ALLOWED_ERR) {
  82                 System.out.println(e.getMessage());
  83                 success = true;
  84             } else {
  85                 System.out.println("should throw DOMException.NO_MODIFICATION_ALLOWED_ERR (7). The error returned is " + e.code);
  86             }
  87         }
  88         if (!success) {
  89             Assert.fail("should throw DOMException.NO_MODIFICATION_ALLOWED_ERR (7).");
  90         }
  91     }
  92 
  93     class DocBuilderWrapper {
  94 
  95         private DocumentBuilder docBuilder;
  96         private final String ENCODING = "UTF-8";
  97 
  98         public DocBuilderWrapper() throws ParserConfigurationException {
  99             this.docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
 100         }
 101 
 102         public DocBuilderWrapper(DocumentBuilder docBuilder) {
 103             setDocumentBuilder(docBuilder);
 104         }
 105 
 106         public DocumentBuilder getDocumentBuilder() {
 107             return docBuilder;
 108         }
 109 
 110         public void setDocumentBuilder(DocumentBuilder docBuilder) {
 111             if (docBuilder == null) {
 112                 new IllegalArgumentException("DocumentBuilder cannot be null");
 113             }
 114 
 115             this.docBuilder = docBuilder;
 116         }
 117 
 118         public Document parse(String xmlData) throws IOException, SAXException {
 119             if (xmlData == null) {
 120                 new IllegalArgumentException("String cannot be null");
 121             }
 122 
 123             ByteArrayInputStream bis = new ByteArrayInputStream(xmlData.getBytes(ENCODING));
 124             InputStreamReader isr = new InputStreamReader(bis, ENCODING);
 125             InputSource source = new InputSource(isr);
 126             return docBuilder.parse(source);
 127         }
 128     }
 129 
 130 }
 131