1 /* 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 import java.lang.reflect.Constructor; 24 import java.util.prefs.Preferences; 25 import java.util.prefs.PreferencesFactory; 26 27 /* 28 * @test 29 * @bug 8068373 30 * @requires os.family == "linux" | os.family == "solaris" 31 * @summary Ensure writing a code point U+0000 null control character is detected. 32 */ 33 public class CodePointZeroPrefsTest 34 { 35 public static void main(String[] args) throws Exception 36 { 37 int failures = 0; 38 39 // Deliberately reflect so you can reproduce it on any platform. 40 Constructor<? extends PreferencesFactory> constructor = 41 Class.forName("java.util.prefs.FileSystemPreferencesFactory").asSubclass(PreferencesFactory.class).getDeclaredConstructor(); 42 constructor.setAccessible(true); 43 PreferencesFactory factory = constructor.newInstance(); 44 45 Preferences node = factory.userRoot().node("com/acme/testing"); 46 47 // legal key and value 48 try { 49 node.put("a", "1"); 50 } catch (IllegalArgumentException iae) { 51 System.err.println("Unexpected IllegalArgumentException for legal key"); 52 failures++; 53 } 54 55 // illegal key only 56 int numIAEs = 0; 57 try { 58 node.put("a\u0000b", "1"); 59 System.err.println("IllegalArgumentException not thrown for illegal key"); 60 failures++; 61 } catch (IllegalArgumentException iae) { 62 // do nothing 63 } 64 65 // illegal value only 66 numIAEs = 0; 67 try { 68 node.put("ab", "2\u00003"); 69 System.err.println("IllegalArgumentException not thrown for illegal value"); 70 failures++; 71 } catch (IllegalArgumentException iae) { 72 // do nothing 73 } 74 75 // illegal key and value 76 numIAEs = 0; 77 try { 78 node.put("a\u0000b", "2\u00003"); 79 System.err.println("IllegalArgumentException not thrown for illegal entry"); 80 failures++; 81 } catch (IllegalArgumentException iae) { 82 // do nothing 83 } 84 85 if (failures != 0) { 86 throw new RuntimeException("CodePointZeroPrefsTest failed with " 87 + failures + " errors!"); 88 } 89 } 90 }