test/jdk/java/util/prefs/CodePointZeroPrefsTest.java
author jdv
Wed, 15 May 2019 10:10:53 +0530
branchmetal-prototype-branch
changeset 57357 f3beca8f19fc
parent 47216 71c04702a3d5
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29397
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
     1
/*
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
     4
 *
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
     8
 *
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    13
 * accompanied this code).
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    14
 *
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    18
 *
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    21
 * questions.
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    22
 */
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    23
import java.lang.reflect.Constructor;
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    24
import java.util.prefs.Preferences;
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    25
import java.util.prefs.PreferencesFactory;
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    26
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    27
/*
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    28
 * @test
30043
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29487
diff changeset
    29
 * @bug 8068373 8075110 8075156
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29487
diff changeset
    30
 * @summary Ensure a code point U+0000 null control character is detected.
31243
625c884323dc 8079539: java/util/prefs/CodePointZeroPrefsTest.java fails with "java.util.prefs.BackingStoreException: Couldn't get file lock."
bpb
parents: 30043
diff changeset
    31
 * @run main/othervm -Djava.util.prefs.userRoot=. CodePointZeroPrefsTest
29397
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    32
 */
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    33
public class CodePointZeroPrefsTest
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    34
{
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    35
    public static void main(String[] args) throws Exception
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    36
    {
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    37
        int failures = 0;
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    38
30043
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29487
diff changeset
    39
        Preferences node = Preferences.userRoot().node("com/acme/testing");
29397
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    40
30043
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29487
diff changeset
    41
        // --- put() ---
29397
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    42
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    43
        // legal key and value
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    44
        try {
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    45
            node.put("a", "1");
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    46
        } catch (IllegalArgumentException iae) {
30043
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29487
diff changeset
    47
            System.err.println("Unexpected IllegalArgumentException for legal put() key");
29397
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    48
            failures++;
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    49
        }
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    50
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    51
        // illegal key only
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    52
        try {
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    53
            node.put("a\u0000b", "1");
30043
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29487
diff changeset
    54
            System.err.println("IllegalArgumentException not thrown for illegal put() key");
29397
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    55
            failures++;
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    56
        } catch (IllegalArgumentException iae) {
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    57
            // do nothing
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    58
        }
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    59
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    60
        // illegal value only
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    61
        try {
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    62
            node.put("ab", "2\u00003");
30043
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29487
diff changeset
    63
            System.err.println("IllegalArgumentException not thrown for illegal put() value");
29397
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    64
            failures++;
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    65
        } catch (IllegalArgumentException iae) {
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    66
            // do nothing
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    67
        }
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    68
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    69
        // illegal key and value
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    70
        try {
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    71
            node.put("a\u0000b", "2\u00003");
30043
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29487
diff changeset
    72
            System.err.println("IllegalArgumentException not thrown for illegal put() entry");
29397
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    73
            failures++;
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    74
        } catch (IllegalArgumentException iae) {
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    75
            // do nothing
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    76
        }
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
    77
30043
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29487
diff changeset
    78
        // --- get ---
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29487
diff changeset
    79
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29487
diff changeset
    80
        // illegal key only
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29487
diff changeset
    81
        try {
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29487
diff changeset
    82
            String theDefault = "default";
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29487
diff changeset
    83
            String value = node.get("a\u0000b", theDefault);
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29487
diff changeset
    84
            System.err.println("IllegalArgumentException not thrown for illegal get() key");
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29487
diff changeset
    85
            failures++;
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29487
diff changeset
    86
        } catch (IllegalArgumentException iae) {
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29487
diff changeset
    87
            // do nothing
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29487
diff changeset
    88
        }
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29487
diff changeset
    89
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29487
diff changeset
    90
        // --- remove ---
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29487
diff changeset
    91
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29487
diff changeset
    92
        // illegal key only
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29487
diff changeset
    93
        try {
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29487
diff changeset
    94
            node.remove("a\u0000b");
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29487
diff changeset
    95
            System.err.println("IllegalArgumentException not thrown for illegal remove() key");
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29487
diff changeset
    96
            failures++;
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29487
diff changeset
    97
        } catch (IllegalArgumentException iae) {
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29487
diff changeset
    98
            // do nothing
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29487
diff changeset
    99
        }
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29487
diff changeset
   100
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29487
diff changeset
   101
        node.removeNode();
b0dd05ec3db1 8075156: (prefs) get*() and remove() should disallow the use of the null control character '\u0000' as key
bpb
parents: 29487
diff changeset
   102
29397
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
   103
        if (failures != 0) {
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
   104
            throw new RuntimeException("CodePointZeroPrefsTest failed with "
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
   105
                + failures + " errors!");
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
   106
        }
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
   107
    }
65faf3bd73ad 8068373: (prefs) FileSystemPreferences writes \0 to XML storage, causing loss of all preferences
bpb
parents:
diff changeset
   108
}