8201279: javax.sound tests should not set java.home system property

Reviewed-by: prr, rriggs
This commit is contained in:
Sergey Bylokhov 2018-05-29 11:22:21 -07:00
parent 9a9dad8b63
commit 5c128bbc7d
14 changed files with 254 additions and 96 deletions
src/java.desktop/share/classes
test/jdk/javax/sound

@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 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
@ -62,13 +62,6 @@ import javax.sound.sampled.spi.MixerProvider;
*/
public final class JDK13Services {
/**
* Filename of the properties file for default provider properties. This
* file is searched in the subdirectory "conf" of the JRE directory (this
* behaviour is hardcoded).
*/
private static final String PROPERTIES_FILENAME = "sound.properties";
/**
* Properties loaded from the properties file for default provider
* properties.
@ -195,7 +188,7 @@ public final class JDK13Services {
private static synchronized Properties getProperties() {
if (properties == null) {
properties = new Properties();
JSSecurityManager.loadProperties(properties, PROPERTIES_FILENAME);
JSSecurityManager.loadProperties(properties);
}
return properties;
}

@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 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
@ -25,10 +25,10 @@
package com.sun.media.sound;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
@ -52,16 +52,6 @@ final class JSSecurityManager {
private JSSecurityManager() {
}
/** Checks if the VM currently has a SecurityManager installed.
* Note that this may change over time. So the result of this method
* should not be cached.
*
* @return true if a SecurityManger is installed, false otherwise.
*/
private static boolean hasSecurityManager() {
return (System.getSecurityManager() != null);
}
static void checkRecordPermission() throws SecurityException {
if(Printer.trace) Printer.trace("JSSecurityManager.checkRecordPermission()");
SecurityManager sm = System.getSecurityManager();
@ -70,69 +60,48 @@ final class JSSecurityManager {
}
}
/** Load properties from a file.
This method tries to load properties from the filename give into
the passed properties object.
If the file cannot be found or something else goes wrong,
the method silently fails.
@param properties The properties bundle to store the values of the
properties file.
@param filename The filename of the properties file to load. This
filename is interpreted as relative to the subdirectory "conf" in
the JRE directory.
/**
* Load properties from a file.
* <p>
* This method tries to load properties from the filename give into the
* passed properties object. If the file cannot be found or something else
* goes wrong, the method silently fails.
* <p>
* If the file referenced in "javax.sound.config.file" property exists and
* the user has an access to it, then it will be loaded, otherwise default
* configuration file "JAVA_HOME/conf/sound.properties" will be loaded.
*
* @param properties the properties bundle to store the values of the
* properties file
*/
static void loadProperties(final Properties properties,
final String filename) {
if(hasSecurityManager()) {
try {
// invoke the privileged action using 1.2 security
PrivilegedAction<Void> action = new PrivilegedAction<Void>() {
@Override
public Void run() {
loadPropertiesImpl(properties, filename);
return null;
}
};
AccessController.doPrivileged(action);
if(Printer.debug)Printer.debug("Loaded properties with JDK 1.2 security");
} catch (Exception e) {
if(Printer.debug)Printer.debug("Exception loading properties with JDK 1.2 security");
// try without using JDK 1.2 security
loadPropertiesImpl(properties, filename);
static void loadProperties(final Properties properties) {
final String customFile = AccessController.doPrivileged(
(PrivilegedAction<String>) () -> System.getProperty(
"javax.sound.config.file"));
if (customFile != null) {
if (loadPropertiesImpl(properties, customFile)) {
return;
}
} else {
// not JDK 1.2 security, assume we already have permission
loadPropertiesImpl(properties, filename);
}
}
private static void loadPropertiesImpl(Properties properties,
String filename) {
if(Printer.trace)Printer.trace(">> JSSecurityManager: loadPropertiesImpl()");
String fname = System.getProperty("java.home");
try {
if (fname == null) {
AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
final String home = System.getProperty("java.home");
if (home == null) {
throw new Error("Can't find java.home ??");
}
File f = new File(fname, "conf");
f = new File(f, filename);
fname = f.getCanonicalPath();
InputStream in = new FileInputStream(fname);
BufferedInputStream bin = new BufferedInputStream(in);
try {
properties.load(bin);
} finally {
if (in != null) {
in.close();
}
}
} catch (Throwable t) {
if (Printer.trace) {
System.err.println("Could not load properties file \"" + fname + "\"");
t.printStackTrace();
}
loadPropertiesImpl(properties, home, "conf", "sound.properties");
return null;
});
}
private static boolean loadPropertiesImpl(final Properties properties,
String first, String... more) {
final Path fname = Paths.get(first, more);
try (final Reader reader = Files.newBufferedReader(fname)) {
properties.load(reader);
return true;
} catch (final Throwable t) {
return false;
}
if(Printer.trace)Printer.trace("<< JSSecurityManager: loadPropertiesImpl() completed");
}
/** Create a Thread in the current ThreadGroup.

@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 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
@ -66,7 +66,9 @@ import com.sun.media.sound.ReferenceCountingDevice;
* Properties can be used to specify default MIDI devices. Both system
* properties and a properties file are considered. The "sound.properties"
* properties file is read from an implementation-specific location (typically
* it is the {@code conf} directory in the Java installation directory). If a
* it is the {@code conf} directory in the Java installation directory).
* The optional "javax.sound.config.file" system property can be used to specify
* the properties file that will be read as the initial configuration. If a
* property exists both as a system property and in the properties file, the
* system property takes precedence. If none is specified, a suitable default is
* chosen among the available devices. The syntax of the properties file is

@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 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
@ -64,7 +64,9 @@ import com.sun.media.sound.JDK13Services;
* Both system properties and a properties file are considered. The
* "sound.properties" properties file is read from an implementation-specific
* location (typically it is the {@code conf} directory in the Java installation
* directory). If a property exists both as a system property and in the
* directory). The optional "javax.sound.config.file" system property can be
* used to specify the properties file that will be read as the initial
* configuration. If a property exists both as a system property and in the
* properties file, the system property takes precedence. If none is specified,
* a suitable default is chosen among the available devices. The syntax of the
* properties file is specified in

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 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
@ -21,16 +21,17 @@
* questions.
*/
import java.io.File;
import java.nio.file.Paths;
import com.sun.media.sound.JDK13Services;
/**
* @test
* @bug 4776511
* @bug 4776511 8201279
* @summary RFE: Setting the default MixerProvider. Test the retrieving and
* parsing of properties. This is a part of the test for 4776511.
* @run main/othervm DefaultProperties
* @run main/othervm/policy=java.policy DefaultProperties
* @modules java.desktop/com.sun.media.sound
*/
public class DefaultProperties {
@ -44,8 +45,10 @@ public class DefaultProperties {
public static void main(String[] args) throws Exception {
boolean allOk = true;
File file = new File(System.getProperty("test.src", "."), "testdata");
System.setProperty("java.home", file.getCanonicalPath());
String path = Paths.get(System.getProperty("test.src", "."),
"testdata", "conf", "sound.properties")
.toAbsolutePath().normalize().toString();
System.setProperty("javax.sound.config.file", path);
for (int i = 0; i < lineTypeClasses.length; i++) {
Class cls = lineTypeClasses[i];

@ -0,0 +1,79 @@
/*
* Copyright (c) 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.
*
* 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.
*/
import java.nio.file.Paths;
import com.sun.media.sound.JDK13Services;
/**
* @test
* @bug 8201279
* @summary this test checks that "javax.sound.config.file" will be ignored if
* the user has no access to the properties file
* @run main/othervm/policy=negative.policy DefaultPropertiesNegative
* @modules java.desktop/com.sun.media.sound
*/
public class DefaultPropertiesNegative {
private static final Class[] lineTypeClasses = {
javax.sound.midi.Receiver.class,
javax.sound.midi.Transmitter.class,
javax.sound.midi.Sequencer.class,
javax.sound.midi.Synthesizer.class,
};
public static void main(String[] args) throws Exception {
boolean allOk = true;
String path = Paths.get(System.getProperty("test.src", "."),
"testdata", "conf", "sound.properties")
.toAbsolutePath().normalize().toString();
System.setProperty("javax.sound.config.file", path);
for (int i = 0; i < lineTypeClasses.length; i++) {
Class cls = lineTypeClasses[i];
// properties file, both provider class name and instance name
String result = JDK13Services.getDefaultProviderClassName(cls);
if (result != null) {
out("type " + cls + " failed: provider class should be 'null' "
+ "but is '" + result + "'!");
allOk = false;
}
result = JDK13Services.getDefaultInstanceName(cls);
if (result != null) {
out("type " + cls + " failed: instance name should be 'null' "
+ "but is '" + result + "'!");
allOk = false;
}
}
if (! allOk) {
throw new Exception("Test failed");
} else {
out("Test passed");
}
}
private static void out(String message) {
System.out.println(message);
}
}

@ -0,0 +1,10 @@
grant {
permission java.util.PropertyPermission "javax.sound.config.file", "write";
permission java.util.PropertyPermission "test.src", "read";
permission java.util.PropertyPermission "javax.sound.midi.Receiver", "write";
permission java.util.PropertyPermission "javax.sound.midi.Transmitter", "write";
permission java.util.PropertyPermission "javax.sound.midi.Sequencer", "write";
permission java.util.PropertyPermission "javax.sound.midi.Synthesizer", "write";
permission java.lang.RuntimePermission "accessClassInPackage.com.sun.media.sound";
permission java.io.FilePermission "${test.src}/testdata/conf/sound.properties", "read";
};

@ -0,0 +1,5 @@
grant {
permission java.util.PropertyPermission "javax.sound.config.file", "write";
permission java.util.PropertyPermission "test.src", "read";
permission java.lang.RuntimePermission "accessClassInPackage.com.sun.media.sound";
};

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 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
@ -21,15 +21,15 @@
* questions.
*/
import java.io.File;
import java.nio.file.Paths;
import com.sun.media.sound.JDK13Services;
/**
* @test
* @bug 4776511
* @build DefaultProperties
* @bug 4776511 8201279
* @run main/othervm DefaultProperties
* @run main/othervm/policy=java.policy DefaultProperties
* @summary RFE: Setting the default MixerProvider. Test the retrieving and
* parsing of properties.
* @modules java.desktop/com.sun.media.sound
@ -45,8 +45,10 @@ public class DefaultProperties {
public static void main(String[] args) throws Exception {
boolean allOk = true;
File file = new File(System.getProperty("test.src", "."), "testdata");
System.setProperty("java.home", file.getCanonicalPath());
String path = Paths.get(System.getProperty("test.src", "."),
"testdata", "conf", "sound.properties")
.toAbsolutePath().normalize().toString();
System.setProperty("javax.sound.config.file", path);
for (int i = 0; i < lineTypeClasses.length; i++) {
Class cls = lineTypeClasses[i];

@ -0,0 +1,78 @@
/*
* Copyright (c) 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.
*
* 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.
*/
import java.nio.file.Paths;
import com.sun.media.sound.JDK13Services;
/**
* @test
* @bug 8201279
* @run main/othervm/policy=negative.policy DefaultPropertiesNegative
* @summary this test checks that "javax.sound.config.file" will be ignored if
* the user has no access to the properties file
* @modules java.desktop/com.sun.media.sound
*/
public class DefaultPropertiesNegative {
private static final Class[] lineTypeClasses = {
javax.sound.sampled.SourceDataLine.class,
javax.sound.sampled.TargetDataLine.class,
javax.sound.sampled.Clip.class,
javax.sound.sampled.Port.class,
};
public static void main(String[] args) throws Exception {
boolean allOk = true;
String path = Paths.get(System.getProperty("test.src", "."),
"testdata", "conf", "sound.properties")
.toAbsolutePath().normalize().toString();
System.setProperty("javax.sound.config.file", path);
for (int i = 0; i < lineTypeClasses.length; i++) {
Class cls = lineTypeClasses[i];
// properties file, both provider class name and instance name
String result = JDK13Services.getDefaultProviderClassName(cls);
if (result != null) {
out("type " + cls + " failed: provider class should be 'null' "
+ "but is '" + result + "'!");
allOk = false;
}
result = JDK13Services.getDefaultInstanceName(cls);
if (result != null) {
out("type " + cls + " failed: instance name should be 'null' "
+ "but is '" + result + "'!");
allOk = false;
}
}
if (! allOk) {
throw new Exception("Test failed");
} else {
out("Test passed");
}
}
private static void out(String message) {
System.out.println(message);
}
}

@ -0,0 +1,10 @@
grant {
permission java.util.PropertyPermission "javax.sound.config.file", "write";
permission java.util.PropertyPermission "test.src", "read";
permission java.util.PropertyPermission "javax.sound.sampled.SourceDataLine", "write";
permission java.util.PropertyPermission "javax.sound.sampled.TargetDataLine", "write";
permission java.util.PropertyPermission "javax.sound.sampled.Clip", "write";
permission java.util.PropertyPermission "javax.sound.sampled.Port", "write";
permission java.lang.RuntimePermission "accessClassInPackage.com.sun.media.sound";
permission java.io.FilePermission "${test.src}/testdata/conf/sound.properties", "read";
};

@ -0,0 +1,5 @@
grant {
permission java.util.PropertyPermission "javax.sound.config.file", "write";
permission java.util.PropertyPermission "test.src", "read";
permission java.lang.RuntimePermission "accessClassInPackage.com.sun.media.sound";
};