test/java/util/prefs/CodePointZeroPrefsTest.java

Print this page
rev 11736 : 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,89 **** import java.util.prefs.Preferences; import java.util.prefs.PreferencesFactory; /* * @test ! * @bug 8068373 ! * @requires os.family == "linux" | os.family == "solaris" ! * @summary Ensure writing a code point U+0000 null control character is detected. */ public class CodePointZeroPrefsTest { public static void main(String[] args) throws Exception { int failures = 0; ! // Deliberately reflect so you can reproduce it on any platform. ! Constructor<? extends PreferencesFactory> constructor = ! Class.forName("java.util.prefs.FileSystemPreferencesFactory").asSubclass(PreferencesFactory.class).getDeclaredConstructor(); ! constructor.setAccessible(true); ! PreferencesFactory factory = constructor.newInstance(); ! Preferences node = factory.userRoot().node("com/acme/testing"); // legal key and value try { node.put("a", "1"); } catch (IllegalArgumentException iae) { ! System.err.println("Unexpected IllegalArgumentException for legal key"); failures++; } // illegal key only - int numIAEs = 0; try { node.put("a\u0000b", "1"); ! System.err.println("IllegalArgumentException not thrown for illegal 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"); 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"); failures++; } catch (IllegalArgumentException iae) { // do nothing } if (failures != 0) { throw new RuntimeException("CodePointZeroPrefsTest failed with " + failures + " errors!"); } } --- 24,106 ---- 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. */ 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!"); } }