Skip to content

Commit

Permalink
Add Java 11 support for SAXParserTest as part of javapathfinder#274
Browse files Browse the repository at this point in the history
  • Loading branch information
varad64 committed Jul 7, 2023
1 parent 98f8a78 commit 7d07f31
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ task compileModules(type: JavaCompile) {
'--add-reads', 'java.base=ALL-UNNAMED',
'--patch-module', "java.base=" + file('src/classes/modules/java.base'),
'--patch-module', "java.logging=" + file('src/classes/modules/java.logging'),
'--patch-module', "java.xml=" + file('src/classes/modules/java.xml'),
]
destinationDir = file('build/classes/modules')
}
Expand Down
1 change: 1 addition & 0 deletions src/classes/modules/java.base/java/io/File.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
*/
public class File
{

public static final String separator = System.getProperty("file.separator");
public static final char separatorChar = separator.charAt(0);
public static final String pathSeparator = System.getProperty("path.separator");
Expand Down
28 changes: 28 additions & 0 deletions src/classes/modules/java.base/java/lang/Class.java
Original file line number Diff line number Diff line change
Expand Up @@ -382,4 +382,32 @@ public boolean isSynthetic (){
public Module getModule() {
return module;
}

/**
* Return fully qualified package name
* Added to support SAXParserTest
*
* @return fully qualified package name
*/
public String getPackageName() {
String packageName = this.packageName;
if (packageName == null) {
Class<?> cls = this;
while (cls.isArray()) {
cls = cls.getComponentType();
}
if (cls.isPrimitive()) {
packageName = "java.lang";
} else {
String clsName = cls.getName();
int dot = clsName.lastIndexOf('.');
packageName = (dot != -1) ? clsName.substring(0, dot).intern() : "";
}
this.packageName = packageName;
}
return packageName;
}

private transient String packageName;

}
14 changes: 14 additions & 0 deletions src/classes/modules/java.base/java/lang/String.java
Original file line number Diff line number Diff line change
Expand Up @@ -488,4 +488,18 @@ private static String generateStringByConcatenatingArgs(Object[] args) {
}
return builder.toString();
}


/**
* StringIndexOutOfBoundsException if {@code index} is
* negative or greater than or equal to {@code length}.
*
* Added to support SAXParserTest
*/
static void checkIndex(int index, int length) {
if (index < 0 || index >= length) {
throw new StringIndexOutOfBoundsException("index " + index + ",length " + length);
}
}

}
226 changes: 226 additions & 0 deletions src/classes/modules/java.xml/jdk/xml/internal/SecuritySupport.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
/*
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package jdk.xml.internal;

import java.io.*;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Properties;

/**
* MJI model for the SecuritySupport class
*
* Aims to eliminate the problems caused by
* executing code with privileges in order to extend
* support for SAXParserTest for Java 11
*
*/
public class SecuritySupport {

static final Properties cacheProps = new Properties();
static volatile boolean firstTime = true;

/**
* Creates and returns a new FileInputStream from a file.
* @param file the specified file
* @return the FileInputStream
* @throws FileNotFoundException if the file is not found
*/
public static FileInputStream getFileInputStream(final File file) throws FileNotFoundException {
try {
return AccessController.doPrivileged((PrivilegedExceptionAction<FileInputStream>) () -> new FileInputStream(file));
} catch (PrivilegedActionException e) {
throw (FileNotFoundException) e.getException();
}
}

public static ClassLoader getContextClassLoader() {
return AccessController.doPrivileged((PrivilegedAction<ClassLoader>) () -> {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
if (cl == null)
cl = ClassLoader.getSystemClassLoader();
return cl;
});
}

/**
* Reads a system property
*
* @param propName the name of the property
* @return the value of the property
*/
public static String getSystemProperty(final String propName) {
return System.getProperty(propName);
}

/**
* Reads JAXP system property in this order: system property,
* $java.home/conf/jaxp.properties if the system property is not specified
*
* @param <T> the type of the property value
* @param type the type of the property value
* @param propName the name of the property
* @param defValue the default value
* @return the value of the property, or the default value if no system
* property is found
*/
public static <T> T getJAXPSystemProperty(Class<T> type, String propName, String defValue) {
String value = getJAXPSystemProperty(propName);
if (value == null) {
value = defValue;
}
if (Integer.class.isAssignableFrom(type)) {
return type.cast(Integer.parseInt(value));
} else if (Boolean.class.isAssignableFrom(type)) {
return type.cast(Boolean.parseBoolean(value));
}
return type.cast(value);
}

/**
* Reads JAXP system property in this order: system property,
* $java.home/conf/jaxp.properties if the system property is not specified
*
* @param propName the name of the property
* @return the value of the property
*/
public static String getJAXPSystemProperty(String propName) {
String value = getSystemProperty(propName);
if (value == null) {
value = readJAXPProperty(propName);
}
return value;
}

/**
* Reads the specified property from $java.home/conf/jaxp.properties
*
* @param propName the name of the property
* @return the value of the property
*/
public static String readJAXPProperty(String propName) {
String value = null;
InputStream is = null;
try {
if(firstTime) {
synchronized(cacheProps) {
if(firstTime) {
String configFile = System.getProperty("java.home") + File.separator + "conf" + File.separator + "jaxp.properties";
File f = new File(configFile);
if (f.exists()) {
is = getFileInputStream(f);
cacheProps.load(is);
}
firstTime = false;
}
}
}
value = cacheProps.getProperty(propName);

} catch (IOException ex) {
} finally {
if (is != null) {
try {
is.close();
} catch (IOException ex) {}
}
}

return value;
}

/**
* Check the protocol used in the systemId against allowed protocols
*
* @param systemId the Id of the URI
* @param allowedProtocols a list of allowed protocols separated by comma
* @param accessAny keyword to indicate allowing any protocol
* @return the name of the protocol if rejected, null otherwise
*/
public static String checkAccess(String systemId, String allowedProtocols,
String accessAny) throws IOException {
if (systemId == null || (allowedProtocols != null &&
allowedProtocols.equalsIgnoreCase(accessAny))) {
return null;
}

String protocol;
if (!systemId.contains(":")) {
protocol = "file";
} else {
URL url = new URL(systemId);
protocol = url.getProtocol();
if (protocol.equalsIgnoreCase("jar")) {
String path = url.getPath();
protocol = path.substring(0, path.indexOf(":"));
} else if (protocol.equalsIgnoreCase("jrt")) {
// if the systemId is "jrt" then allow access if "file" allowed
protocol = "file";
}
}

if (isProtocolAllowed(protocol, allowedProtocols)) {
//access allowed
return null;
} else {
return protocol;
}
}

/**
* Check if the protocol is in the allowed list of protocols. The check
* is case-insensitive while ignoring whitespaces.
*
* @param protocol a protocol
* @param allowedProtocols a list of allowed protocols
* @return true if the protocol is in the list
*/
private static boolean isProtocolAllowed(String protocol, String allowedProtocols) {
if (allowedProtocols == null) {
return false;
}
String temp[] = allowedProtocols.split(",");
for (String t : temp) {
t = t.trim();
if (t.equalsIgnoreCase(protocol)) {
return true;
}
}
return false;
}

/**
* Checks whether the file exists.
* @param f the specified file
* @return true if the file exists, false otherwise
*/
private static boolean doesFileExist(File f) {
return f.exists();
}
}
2 changes: 2 additions & 0 deletions src/tests/gov/nasa/jpf/test/xerces/SAXParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.junit.Test;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
Expand Down

0 comments on commit 7d07f31

Please sign in to comment.