test/java/util/prefs/CodePointZeroPrefsTest.java

Print this page
rev 11676 : 8075156: (prefs) remove() should disallow the use of the null control character '\u0000' as key
Summary: Extend disallowing null control character key to remove()
Reviewed-by: XXX

@@ -24,11 +24,11 @@
 import java.util.prefs.Preferences;
 import java.util.prefs.PreferencesFactory;
 
 /*
  * @test
- * @bug 8068373
+ * @bug 8068373 8075110 8075156
  * @requires os.family == "linux" | os.family == "solaris"
  * @summary Ensure writing a code point U+0000 null control character is detected.
  */
 public class CodePointZeroPrefsTest
 {

@@ -42,48 +42,76 @@
         constructor.setAccessible(true);
         PreferencesFactory factory = constructor.newInstance();
 
         Preferences node = factory.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 key");
+            System.err.println("Unexpected IllegalArgumentException for legal put() key");
             failures++;
         }
 
         // illegal key only
-        int numIAEs = 0;
         try {
             node.put("a\u0000b", "1");
-            System.err.println("IllegalArgumentException not thrown for illegal key");
+            System.err.println("IllegalArgumentException not thrown for illegal put() key");
             failures++;
         } catch (IllegalArgumentException iae) {
             // do nothing
         }
 
         // illegal value only
-        numIAEs = 0;
         try {
             node.put("ab", "2\u00003");
-            System.err.println("IllegalArgumentException not thrown for illegal value");
+            System.err.println("IllegalArgumentException not thrown for illegal put() value");
             failures++;
         } catch (IllegalArgumentException iae) {
             // do nothing
         }
 
         // illegal key and value
-        numIAEs = 0;
         try {
             node.put("a\u0000b", "2\u00003");
-            System.err.println("IllegalArgumentException not thrown for illegal entry");
+            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);
+            if (value == null || !value.equals(theDefault)) {
+                System.err.println("Default value not returned for illegal get() key");
+                failures++;
+            }
+        } catch (IllegalArgumentException iae) {
+            // AbstractPreferences returns default if getSpi() throws Exception
+            System.err.println("IllegalArgumentException not thrown for illegal get() key");
+            failures++;
+        }
+
+        // --- 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!");
         }
     }