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 8075110 8075156
  30  * @summary Ensure 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         Preferences node = Preferences.userRoot().node("com/acme/testing");
  39 
  40         // --- put() ---
  41 
  42         // legal key and value
  43         try {
  44             node.put("a", "1");
  45         } catch (IllegalArgumentException iae) {
  46             System.err.println("Unexpected IllegalArgumentException for legal put() key");
  47             failures++;
  48         }
  49 
  50         // illegal key only
  51         try {
  52             node.put("a\u0000b", "1");
  53             System.err.println("IllegalArgumentException not thrown for illegal put() key");
  54             failures++;
  55         } catch (IllegalArgumentException iae) {
  56             // do nothing
  57         }
  58 
  59         // illegal value only
  60         try {
  61             node.put("ab", "2\u00003");
  62             System.err.println("IllegalArgumentException not thrown for illegal put() value");
  63             failures++;
  64         } catch (IllegalArgumentException iae) {
  65             // do nothing
  66         }
  67 
  68         // illegal key and value
  69         try {
  70             node.put("a\u0000b", "2\u00003");
  71             System.err.println("IllegalArgumentException not thrown for illegal put() entry");
  72             failures++;
  73         } catch (IllegalArgumentException iae) {
  74             // do nothing
  75         }
  76 
  77         // --- get ---
  78 
  79         // illegal key only
  80         try {
  81             String theDefault = "default";
  82             String value = node.get("a\u0000b", theDefault);
  83             System.err.println("IllegalArgumentException not thrown for illegal get() key");
  84             failures++;
  85         } catch (IllegalArgumentException iae) {
  86             // do nothing
  87         }
  88 
  89         // --- remove ---
  90 
  91         // illegal key only
  92         try {
  93             node.remove("a\u0000b");
  94             System.err.println("IllegalArgumentException not thrown for illegal remove() key");
  95             failures++;
  96         } catch (IllegalArgumentException iae) {
  97             // do nothing
  98         }
  99 
 100         node.removeNode();
 101 
 102         if (failures != 0) {
 103             throw new RuntimeException("CodePointZeroPrefsTest failed with "
 104                 + failures + " errors!");
 105         }
 106     }
 107 }