|
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 * @summary Ensure writing a code point U+0000 null control character is detected. |
|
31 */ |
|
32 public class CodePointZeroPrefsTest |
|
33 { |
|
34 public static void main(String[] args) throws Exception |
|
35 { |
|
36 int failures = 0; |
|
37 |
|
38 // Deliberately reflect so you can reproduce it on any platform. |
|
39 Constructor<? extends PreferencesFactory> constructor = |
|
40 Class.forName("java.util.prefs.FileSystemPreferencesFactory").asSubclass(PreferencesFactory.class).getDeclaredConstructor(); |
|
41 constructor.setAccessible(true); |
|
42 PreferencesFactory factory = constructor.newInstance(); |
|
43 |
|
44 Preferences node = factory.userRoot().node("com/acme/testing"); |
|
45 |
|
46 // legal key and value |
|
47 try { |
|
48 node.put("a", "1"); |
|
49 } catch (IllegalArgumentException iae) { |
|
50 System.err.println("Unexpected IllegalArgumentException for legal key"); |
|
51 failures++; |
|
52 } |
|
53 |
|
54 // illegal key only |
|
55 int numIAEs = 0; |
|
56 try { |
|
57 node.put("a\u0000b", "1"); |
|
58 System.err.println("IllegalArgumentException not thrown for illegal key"); |
|
59 failures++; |
|
60 } catch (IllegalArgumentException iae) { |
|
61 // do nothing |
|
62 } |
|
63 |
|
64 // illegal value only |
|
65 numIAEs = 0; |
|
66 try { |
|
67 node.put("ab", "2\u00003"); |
|
68 System.err.println("IllegalArgumentException not thrown for illegal value"); |
|
69 failures++; |
|
70 } catch (IllegalArgumentException iae) { |
|
71 // do nothing |
|
72 } |
|
73 |
|
74 // illegal key and value |
|
75 numIAEs = 0; |
|
76 try { |
|
77 node.put("a\u0000b", "2\u00003"); |
|
78 System.err.println("IllegalArgumentException not thrown for illegal entry"); |
|
79 failures++; |
|
80 } catch (IllegalArgumentException iae) { |
|
81 // do nothing |
|
82 } |
|
83 |
|
84 if (failures != 0) { |
|
85 throw new RuntimeException("CodePointZeroPrefsTest failed with " |
|
86 + failures + " errors!"); |
|
87 } |
|
88 } |
|
89 } |