undefect. CWE-407 — 63 sites patched across 27 ecosystems

Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com

Patches, unit tests, benchmarks, whitepaper, and outreach briefs.
Public domain — no copyright claimed. Use freely.
This commit is contained in:
russell@unturf.com 2026-03-26 17:11:57 -04:00
commit 0a580b313d
70422 changed files with 17213626 additions and 0 deletions

View file

@ -0,0 +1,140 @@
/*
* Copyright (c) 2012, 2022, 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.
*/
/* @test
* @bug 7160252 7197662
* @key intermittent
* @summary Checks if events are delivered to a listener
* when a child node is added or removed
* @run main/othervm -Djava.util.prefs.userRoot=. AddNodeChangeListener
*/
import java.util.prefs.*;
public class AddNodeChangeListener {
private static final int SLEEP_ITRS = 10;
private static final String N2_STR = "N2";
private static boolean failed = false;
private static Preferences userRoot, N2;
private static NodeChangeListenerAdd ncla;
public static void main(String[] args)
throws BackingStoreException, InterruptedException {
try {
userRoot = Preferences.userRoot();
// Make sure test node is not present before test
clearPrefs();
ncla = new NodeChangeListenerAdd();
userRoot.addNodeChangeListener(ncla);
//Should initiate a node added event
addNode();
// Should not initiate a node added event
addNode();
//Should initate a child removed event
removeNode();
if (failed) {
throw new RuntimeException("Failed");
}
} finally {
// Make sure test node is not present after the test
clearPrefs();
}
}
private static void addNode()
throws BackingStoreException, InterruptedException {
N2 = userRoot.node(N2_STR);
userRoot.flush();
checkAndSleep(1, "addNode");
}
private static void removeNode()
throws BackingStoreException, InterruptedException {
N2.removeNode();
userRoot.flush();
checkAndSleep(0, "removeNode");
}
/* Check for the expected value in the listener (1 after addNode(), 0 after removeNode()).
* Sleep a few extra times in a loop, if needed, for debugging purposes, to
* see if the event gets delivered late.
*/
private static void checkAndSleep(int expectedListenerVal, String methodName) throws InterruptedException {
int expectedItr = -1; // iteration in which the expected value was retrieved from the listener, or -1 if never
for (int i = 0; i < SLEEP_ITRS; i++) {
System.out.print(methodName + " sleep iteration " + i + "...");
Thread.sleep(3000);
System.out.println("done.");
if (ncla.getAddNumber() == expectedListenerVal) {
expectedItr = i;
break;
}
}
if (expectedItr == 0) {
System.out.println(methodName + " test passed");
} else {
failed = true;
if (expectedItr == -1) {
System.out.println("Failed in " + methodName + " - change listener never notified");
} else {
System.out.println("Failed in " + methodName + " - listener notified on iteration " + expectedItr);
}
}
}
/* Check if the node already exists in userRoot, and remove it if so. */
private static void clearPrefs() throws BackingStoreException {
if (userRoot.nodeExists(N2_STR)) {
System.out.println("Node " + N2_STR + " already/still exists; clearing");
Preferences clearNode = userRoot.node(N2_STR);
userRoot.flush();
clearNode.removeNode();
userRoot.flush();
if (userRoot.nodeExists(N2_STR)) {
throw new RuntimeException("Unable to clear pre-existing node." + (failed ? " Also, the test failed" : ""));
}
}
}
private static class NodeChangeListenerAdd implements NodeChangeListener {
private int totalNode = 0;
@Override
public void childAdded(NodeChangeEvent evt) {
totalNode++;
}
@Override
public void childRemoved(NodeChangeEvent evt) {
totalNode--;
}
public int getAddNumber(){
return totalNode;
}
}
}

View file

@ -0,0 +1,39 @@
/*
* Copyright (c) 2012, 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.util.prefs.Preferences;
/**
* First class called by CheckUserPrefsStorage.java to create and
* store a user preference
*/
public class CheckUserPrefFirst {
public static void main(String[] args) throws Exception {
Preferences prefs = Preferences.userNodeForPackage(CheckUserPrefFirst.class);
prefs.put("Check", "Success");
prefs.flush();
}
}

View file

@ -0,0 +1,43 @@
/*
* Copyright (c) 2012, 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.util.prefs.Preferences;
/**
* CheckUserPrefsStorage.java uses this to check that preferences stored
* by CheckUserPrefFirst.java can be retrieved
*/
public class CheckUserPrefLater {
public static void main(String[] args) throws Exception {
Preferences prefs = Preferences.userNodeForPackage(CheckUserPrefFirst.class);
String result = prefs.get("Check", null);
if ((result == null) || !(result.equals("Success")))
throw new RuntimeException("User pref not stored!");
prefs.remove("Check");
prefs.flush();
}
}

View file

@ -0,0 +1,50 @@
/*
* Copyright (c) 2018, 2024, 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.
*/
/*
* @test
* @bug 7198073 7197662
* @summary Tests that user preferences are stored in the permanent storage
* @library /test/lib
* @build jdk.test.lib.process.* CheckUserPrefFirst CheckUserPrefLater
* @run main CheckUserPrefsStorage
*/
import jdk.test.lib.process.ProcessTools;
public class CheckUserPrefsStorage {
public static void main(String[] args) throws Throwable {
// First to create and store a user preference
run("CheckUserPrefFirst");
// Then check that preferences stored by CheckUserPrefFirst can be retrieved
run("CheckUserPrefLater");
}
public static void run(String testName) throws Exception {
ProcessTools.executeTestJava("-Djava.util.prefs.userRoot=.", testName)
.outputTo(System.out)
.errorTo(System.out)
.shouldHaveExitValue(0);
}
}

View file

@ -0,0 +1,108 @@
/*
* Copyright (c) 2015, 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.lang.reflect.Constructor;
import java.util.prefs.Preferences;
import java.util.prefs.PreferencesFactory;
/*
* @test
* @bug 8068373 8075110 8075156
* @summary Ensure a code point U+0000 null control character is detected.
* @run main/othervm -Djava.util.prefs.userRoot=. CodePointZeroPrefsTest
*/
public class CodePointZeroPrefsTest
{
public static void main(String[] args) throws Exception
{
int failures = 0;
Preferences node = Preferences.userRoot().node("com/acme/testing");
// --- put() ---
// legal key and value
try {
node.put("a", "1");
} catch (IllegalArgumentException iae) {
System.err.println("Unexpected IllegalArgumentException for legal put() key");
failures++;
}
// illegal key only
try {
node.put("a\u0000b", "1");
System.err.println("IllegalArgumentException not thrown for illegal put() key");
failures++;
} catch (IllegalArgumentException iae) {
// do nothing
}
// illegal value only
try {
node.put("ab", "2\u00003");
System.err.println("IllegalArgumentException not thrown for illegal put() value");
failures++;
} catch (IllegalArgumentException iae) {
// do nothing
}
// illegal key and value
try {
node.put("a\u0000b", "2\u00003");
System.err.println("IllegalArgumentException not thrown for illegal put() entry");
failures++;
} catch (IllegalArgumentException iae) {
// do nothing
}
// --- get ---
// illegal key only
try {
String theDefault = "default";
String value = node.get("a\u0000b", theDefault);
System.err.println("IllegalArgumentException not thrown for illegal get() key");
failures++;
} catch (IllegalArgumentException iae) {
// do nothing
}
// --- remove ---
// illegal key only
try {
node.remove("a\u0000b");
System.err.println("IllegalArgumentException not thrown for illegal remove() key");
failures++;
} catch (IllegalArgumentException iae) {
// do nothing
}
node.removeNode();
if (failures != 0) {
throw new RuntimeException("CodePointZeroPrefsTest failed with "
+ failures + " errors!");
}
}
}

View file

@ -0,0 +1,61 @@
/*
* Copyright (c) 2009, 2012, 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.
*/
/*
* @test
* @bug 4619564 7197662
* @summary XMl Comments in Preferences File lead to ClassCastException
* @run main/othervm -Djava.util.prefs.userRoot=. CommentsInXml
* @author kladko
*/
import java.io.*;
import java.util.prefs.*;
public class CommentsInXml {
public static void main(String[] argv) throws Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bos.write(new String(
"<!DOCTYPE preferences SYSTEM " +
"\"http://java.sun.com/dtd/preferences.dtd\"> " +
"<preferences EXTERNAL_XML_VERSION=\"1.0\"> " +
" <root type=\"user\"> " +
" <map> " +
" </map> " +
" <node name=\"hlrAgent\"> <!-- HLR Agent --> " +
" <map> " +
" <entry key=\"agentName\" value=\"HLRAgent\" />" +
" </map> " +
" </node> " +
" </root> " +
"</preferences> "
).getBytes());
Preferences ur = Preferences.userRoot();
ur.importPreferences(new ByteArrayInputStream(bos.toByteArray()));
ur.node("hlrAgent").removeNode(); // clean
}
}

View file

@ -0,0 +1,50 @@
/*
* Copyright (c) 2009, 2012, 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.
*/
/*
* @test
* @bug 4703132 7197662
* @summary flush() throws an IllegalStateException on a removed node
* @run main/othervm -Djava.util.prefs.userRoot=. ConflictInFlush
* @author Sucheta Dambalkar
*/
import java.util.prefs.*;
public final class ConflictInFlush{
public static void main(String args[]) {
Preferences root = Preferences.userRoot();
try {
Preferences node = root.node("1/2/3");
node.flush();
System.out.println("Node "+node+" has been created");
System.out.println("Removing node "+node);
node.removeNode();
node.flush();
}catch (BackingStoreException bse){
bse.printStackTrace();
}
}
}

View file

@ -0,0 +1,75 @@
/*
* Copyright (c) 2009, 2021, 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.
*/
/*
* @test
* @bug 4387136 4947349 7197662
* @summary Due to a bug in XMLSupport.putPreferencesInXml(...),
* node's keys would not get exported.
* @run main/othervm -Djava.util.prefs.userRoot=. ExportNode
* @author Konstantin Kladko
*/
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
public class ExportNode {
private static final String NODE_NAME_1 = "ExportNodeTestName1";
private static final String NODE_NAME_2 = "ExportNodeTestName2";
public static void main(String[] args) throws
BackingStoreException, IOException {
Preferences N1 = Preferences.userRoot().node("ExportNodeTest1");
N1.put(NODE_NAME_1,"ExportNodeTestValue1");
Preferences N2 = N1.node("ExportNodeTest2");
N2.put(NODE_NAME_2,"ExportNodeTestValue2");
ByteArrayOutputStream exportStream = new ByteArrayOutputStream();
N2.exportNode(exportStream);
// Removal of preference node should always succeed on Solaris/Linux
// by successfully acquiring the appropriate file lock (4947349)
N1.removeNode();
String streamAsString = exportStream.toString("UTF-8");
StringBuilder sb = null;
if (streamAsString.lastIndexOf(NODE_NAME_2) == -1) {
if (sb == null)
sb = new StringBuilder();
sb.append(NODE_NAME_2 + " should have been found");
}
if (streamAsString.lastIndexOf(NODE_NAME_1) != -1) {
if (sb == null)
sb = new StringBuilder();
else
sb.append("; ");
sb.append(NODE_NAME_1 + " should *not* have been found");
}
if (sb != null)
throw new RuntimeException(sb.toString());
}
}

View file

@ -0,0 +1,94 @@
/*
* Copyright (c) 2009, 2019, 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.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.prefs.Preferences;
/**
* @test
* @bug 6203576 4700020 7197662 8217777
* @summary checks if the output of exportSubtree() is identical to
* the output from previous release.
* @run main/othervm -Djava.util.prefs.userRoot=. ExportSubtree
*/
public class ExportSubtree {
private static final String LS = System.getProperty("line.separator");
private static final String IMPORT_PREFS =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<!DOCTYPE preferences SYSTEM \"http://java.sun.com/dtd/preferences.dtd\">" +
"<preferences EXTERNAL_XML_VERSION=\"1.0\">" +
" <root type=\"user\">" +
" <map>" +
" <entry key=\"key1\" value=\"value1\"/>" +
" </map>" +
" <node name=\"testExportSubtree\">" +
" <map>" +
" <entry key=\"key2\" value=\"value2\"/>" +
" </map>" +
" <node name=\"test\">" +
" <map>" +
" <entry key=\"key3\" value=\"value3\"/>" +
" </map>" +
" </node>" +
" </node>" +
" </root>" +
"</preferences>";
private static final String EXPECTED_RESULT =
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" + LS +
"<!DOCTYPE preferences SYSTEM \"http://java.sun.com/dtd/preferences.dtd\">" + LS +
"<preferences EXTERNAL_XML_VERSION=\"1.0\">" + LS +
" <root type=\"user\">" + LS +
" <map/>" + LS +
" <node name=\"testExportSubtree\">" + LS +
" <map>" + LS +
" <entry key=\"key2\" value=\"value2\"/>" + LS +
" </map>" + LS +
" <node name=\"test\">" + LS +
" <map>" + LS +
" <entry key=\"key3\" value=\"value3\"/>" + LS +
" </map>" + LS +
" </node>" + LS +
" </node>" + LS +
" </root>" + LS +
"</preferences>" + LS;
public static void main(String[] args) throws Exception {
ByteArrayInputStream bais = new ByteArrayInputStream(IMPORT_PREFS.getBytes("utf-8"));
Preferences.importPreferences(bais);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Preferences.userRoot().node("testExportSubtree").exportSubtree(baos);
Preferences.userRoot().node("testExportSubtree").removeNode();
if (!EXPECTED_RESULT.equals(baos.toString())) {
String errMsg = "Preferences::exportSubtree did not yield the expected result.";
System.out.println(errMsg + LS +
"Actual:" + LS +
baos + LS +
"Expected:" + LS +
EXPECTED_RESULT);
throw new RuntimeException(errMsg);
}
}
}

View file

@ -0,0 +1,44 @@
/*
* Copyright (c) 2009, 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.util.prefs.Preferences;
/*
* main class used by regtest PrefsSpiTest.java
*/
public class PrefsSpi {
public static void main (String[] args) throws Exception {
if (args.length != 1)
throw new Exception("Usage: java PrefsSpi REGEXP");
String className = Preferences.userRoot().getClass().getName();
System.out.printf("className=%s%n", className);
if (! className.matches(args[0]))
throw new Exception("Preferences class name \"" + className
+ "\" does not match regular expression \""
+ args[0] + "\".");
}
}

View file

@ -0,0 +1,109 @@
/*
* Copyright (c) 2018, 2026, 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.
*/
/*
* @test
* @bug 4991526 6514993 7197662
* @summary Unit test for Preferences jar providers
* @library /test/lib
* @build jdk.test.lib.util.JarUtils jdk.test.lib.process.*
* PrefsSpi StubPreferencesFactory StubPreferences
* @run junit PrefsSpiTest
*/
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import jdk.test.lib.JDKToolFinder;
import jdk.test.lib.Utils;
import jdk.test.lib.process.ProcessTools;
import jdk.test.lib.util.JarUtils;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import static java.nio.file.StandardOpenOption.CREATE;
import static java.util.Arrays.asList;
import static jdk.test.lib.Utils.TEST_CLASSES;
import static jdk.test.lib.Utils.TEST_CLASS_PATH;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
public class PrefsSpiTest {
private static final Path SPIJAR = Path.of("extDir", "PrefsSpi.jar");
private static final String SPIJAR_CP = TEST_CLASS_PATH
+ File.pathSeparator + SPIJAR.toString();
@BeforeAll
public static void initialize() throws Exception {
Path xdir = Path.of("jarDir");
Path config = xdir.resolve("META-INF/services/java.util.prefs.PreferencesFactory");
Files.createDirectories(config.getParent());
Files.write(config, "StubPreferencesFactory".getBytes(), CREATE);
String[] files = {"StubPreferencesFactory.class", "StubPreferences.class"};
for (String f : files) {
Path source = Path.of(TEST_CLASSES, f);
Path target = xdir.resolve(source.getFileName());
Files.copy(source, target, REPLACE_EXISTING);
}
JarUtils.createJarFile(SPIJAR, xdir);
}
public static Stream<Arguments> testCases() {
return Stream.of
(// CLI options, runtime arguments
Arguments.of(List.of("-cp", SPIJAR_CP,
"-Djava.util.prefs.PreferencesFactory=StubPreferencesFactory"),
"StubPreferences"),
Arguments.of(List.of("-cp", TEST_CLASS_PATH), "java.util.prefs.*"),
Arguments.of(List.of("-cp", SPIJAR_CP), "StubPreferences"));
}
@ParameterizedTest
@MethodSource("testCases")
public void testProvider(List<String> opts, String pattern) throws Throwable {
List<String> args = new ArrayList<>();
args.add(JDKToolFinder.getJDKTool("java"));
args.addAll(asList(Utils.getTestJavaOpts()));
args.addAll(opts);
args.add("-Djava.util.prefs.userRoot=.");
args.add(PrefsSpi.class.getName());
args.add(pattern);
ProcessTools.executeCommand(args.stream()
.filter(t -> !t.isEmpty())
.toArray(String[]::new))
.shouldHaveExitValue(0);
}
}

View file

@ -0,0 +1,99 @@
/*
* Copyright (c) 2012, 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.
*/
/* @test
* @bug 7160242 7165118 7197662
* @summary Check if NullPointerException is thrown if the key passed
* to remove() is null.
* @run main/othervm -Djava.util.prefs.userRoot=. RemoveNullKeyCheck
*/
import java.util.prefs.Preferences;
import java.util.prefs.AbstractPreferences;
import java.util.prefs.BackingStoreException;
public class RemoveNullKeyCheck {
private static boolean failed = false;
public static void main(String[] args) throws Exception {
checkPreferencesRemove();
checkAbstractPreferencesRemove();
if (failed) {
throw new RuntimeException("Expected NullPointerException " +
"not thrown");
}
}
public static void checkPreferencesRemove() {
try {
Preferences node = Preferences.userRoot().node("N1");
node.remove(null);
failed = true;
} catch (NullPointerException npe) {
}
}
public static void checkAbstractPreferencesRemove() {
Preferences abstrPrefs = new AbstractPreferences(null, "") {
@Override
protected void putSpi(String key, String value) {
}
@Override
protected String getSpi(String key) {
return null;
}
@Override
protected void removeSpi(String key) {
}
@Override
protected void removeNodeSpi() throws BackingStoreException {
}
@Override
protected String[] keysSpi() throws BackingStoreException {
return new String[0];
}
@Override
protected String[] childrenNamesSpi() throws BackingStoreException {
return new String[0];
}
@Override
protected AbstractPreferences childSpi(String name) {
return null;
}
@Override
protected void syncSpi() throws BackingStoreException {
}
@Override
protected void flushSpi() throws BackingStoreException {
}
};
try {
abstrPrefs.remove(null);
failed = true;
} catch(NullPointerException npe) {
}
}
}

View file

@ -0,0 +1,64 @@
/*
* Copyright (c) 2009, 2012, 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.
*/
/* @test
* @bug 6178148 7197662
* @summary check if wrong exception gets thrown if one of the child
* nodes is readonly on underlying filesystem when node is
* being removed.
* @run main/othervm -Djava.util.prefs.userRoot=. RemoveReadOnlyNode
*/
import java.io.*;
import java.util.prefs.*;
public class RemoveReadOnlyNode {
public static void main(String[] args) throws Exception {
String osName = System.getProperty("os.name");
if (osName.startsWith("Windows"))
return;
Preferences root = Preferences.userRoot();
Preferences node1 = root.node("node1");
Preferences node1A = node1.node("node1A");
Preferences node1B = node1.node("node1B");
node1B.put("mykey", "myvalue");
node1.flush();
String node1BDirName = System.getProperty("user.home")
+ "/.java/.userPrefs"
+ "/node1/node1B";
File node1BDir = new File(node1BDirName);
node1BDir.setReadOnly();
try {
node1.removeNode();
}
catch (BackingStoreException ex) {
//expected exception
} finally {
Runtime.getRuntime().exec("chmod 755 " + node1BDirName).waitFor();
try {
node1.removeNode();
} catch (Exception e) {}
}
}
}

View file

@ -0,0 +1,64 @@
/*
* Copyright (c) 2009, 2012, 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.
*/
/* @test
* @bug 4705094 7197662
* @summary Checks if correct exception gets thrown when removing an
* unregistered NodeChangeListener .
* @run main/othervm -Djava.util.prefs.userRoot=. RemoveUnregedListener
*/
import java.util.prefs.*;
import java.util.*;
public class RemoveUnregedListener {
public static void main(String[] args) throws Exception {
Preferences userRoot = null;
Preferences N1 = null;
NodeChangeListenerTestAdd ncl = new NodeChangeListenerTestAdd();
NodeChangeListenerTestAdd ncl2 = new NodeChangeListenerTestAdd();
NodeChangeListenerTestAdd ncl3 = new NodeChangeListenerTestAdd();
try {
userRoot = Preferences.userRoot();
N1 = userRoot.node("N1");
userRoot.flush();
//add ncl nc2
N1.addNodeChangeListener(ncl);
N1.addNodeChangeListener(ncl2);
N1.removeNodeChangeListener(ncl3);
throw new RuntimeException();
} catch (IllegalArgumentException iae) {
System.out.println("Test Passed!");
} catch (Exception e) {
System.out.println("Test Failed");
throw e;
}
}
}
class NodeChangeListenerTestAdd implements NodeChangeListener {
public void childAdded(NodeChangeEvent evt) {}
public void childRemoved(NodeChangeEvent evt) {}
}

View file

@ -0,0 +1,48 @@
/*
* Copyright (c) 2009, 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.
*/
/*
* @test
* @bug 4811356
* @summary Prefs exceptions were unintentionally not serializable
* @author Josh Bloch
*/
import java.util.prefs.*;
import java.io.*;
public class SerializeExceptions {
public static void main(String args[]) throws Exception {
test(new BackingStoreException("Hi"));
test(new InvalidPreferencesFormatException("Mom!"));
}
static void test(Object o) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bos);
out.writeObject(o);
out.flush();
out.close();
}
}

View file

@ -0,0 +1,37 @@
/*
* 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.util.prefs.AbstractPreferences;
public class StubPreferences extends AbstractPreferences {
public StubPreferences() { super(null, ""); }
public String getSpi(String x) { return null; }
public void putSpi(String x, String y) { }
public void removeSpi(String x) { }
public AbstractPreferences childSpi(String x) { return null; }
public void removeNodeSpi() { }
public String[] keysSpi() { return null; }
public String[] childrenNamesSpi() { return null; }
public void syncSpi() { }
public void flushSpi() { }
}

View file

@ -0,0 +1,30 @@
/*
* 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.util.prefs.Preferences;
import java.util.prefs.PreferencesFactory;
public class StubPreferencesFactory implements PreferencesFactory {
public Preferences userRoot() { return new StubPreferences(); }
public Preferences systemRoot() { return new StubPreferences(); }
}

View file

@ -0,0 +1 @@
modules = java.prefs