1 /*
   2  * Copyright (c) 2012, 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 
  24 /*
  25  * @test
  26  * @bug 8000354 8000685 8004371
  27  * @summary Basic test of storeToXML and loadToXML
  28  * @run main LoadAndStoreXML
  29  * @run main/othervm -Dsun.util.spi.XmlPropertiesProvider=jdk.internal.util.xml.BasicXmlPropertiesProvider LoadAndStoreXML
  30  */
  31 
  32 import java.io.*;
  33 import java.util.*;
  34 import java.security.*;
  35 
  36 public class LoadAndStoreXML {
  37 
  38     /**
  39      * Simple policy implementation that grants a set of permissions to
  40      * all code sources and protection domains.
  41      */
  42     static class SimplePolicy extends Policy {
  43         private final Permissions perms;
  44 
  45         public SimplePolicy(Permission...permissions) {
  46             perms = new Permissions();
  47             for (Permission permission : permissions)
  48                 perms.add(permission);
  49         }
  50 
  51         @Override
  52         public PermissionCollection getPermissions(CodeSource cs) {
  53             return perms;
  54         }
  55 
  56         @Override
  57         public PermissionCollection getPermissions(ProtectionDomain pd) {
  58             return perms;
  59         }
  60 
  61         @Override
  62         public boolean implies(ProtectionDomain pd, Permission p) {
  63             return perms.implies(p);
  64         }
  65     }
  66 
  67     /**
  68      * Sanity test that properties saved with Properties#storeToXML can be
  69      * read with Properties#loadFromXML.
  70      */
  71     static void testLoadAndStore(String encoding) throws IOException {
  72         Properties props = new Properties();
  73         props.put("k1", "foo");
  74         props.put("k2", "bar");
  75 
  76         ByteArrayOutputStream out = new ByteArrayOutputStream();
  77         props.storeToXML(out, null, encoding);
  78 
  79         Properties p = new Properties();
  80         ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
  81         p.loadFromXML(in);
  82 
  83         if (!p.equals(props)) {
  84             System.err.println("stored: " + props);
  85             System.err.println("loaded: " + p);
  86             throw new RuntimeException("Test failed");
  87         }
  88     }
  89 
  90     /**
  91      * Test loadFromXML with a document that does not have an encoding declaration
  92      */
  93     static void testLoadWithoutEncoding() throws IOException {
  94         Properties expected = new Properties();
  95         expected.put("foo", "bar");
  96 
  97         String s = "<?xml version=\"1.0\"?>" +
  98                    "<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">" +
  99                    "<properties>" +
 100                    "<entry key=\"foo\">bar</entry>" +
 101                    "</properties>";
 102         ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes("UTF-8"));
 103         Properties props = new Properties();
 104         props.loadFromXML(in);
 105 
 106         if (!props.equals(expected)) {
 107             System.err.println("loaded: " + props + ", expected: " + expected);
 108             throw new RuntimeException("Test failed");
 109         }
 110     }
 111 
 112      /**
 113       * Test loadFromXML with unsupported encoding
 114       */
 115      static void testLoadWithBadEncoding() throws IOException {
 116         String s = "<?xml version=\"1.0\" encoding=\"BAD\"?>" +
 117                    "<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">" +
 118                    "<properties>" +
 119                    "<entry key=\"foo\">bar</entry>" +
 120                    "</properties>";
 121         ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes("UTF-8"));
 122         Properties props = new Properties();
 123         try {
 124             props.loadFromXML(in);
 125             throw new RuntimeException("UnsupportedEncodingException expected");
 126         } catch (UnsupportedEncodingException expected) { }
 127     }
 128 
 129     /**
 130      * Test storeToXML with unsupported encoding
 131      */
 132     static void testStoreWithBadEncoding() throws IOException {
 133         Properties props = new Properties();
 134         props.put("foo", "bar");
 135         ByteArrayOutputStream out = new ByteArrayOutputStream();
 136         try {
 137             props.storeToXML(out, null, "BAD");
 138             throw new RuntimeException("UnsupportedEncodingException expected");
 139         } catch (UnsupportedEncodingException expected) { }
 140     }
 141 
 142     public static void main(String[] args) throws IOException {
 143 
 144         testLoadAndStore("UTF-8");
 145         testLoadAndStore("UTF-16");
 146         testLoadWithoutEncoding();
 147         testLoadWithBadEncoding();
 148         testStoreWithBadEncoding();
 149 
 150         // re-run sanity test with security manager
 151         Policy orig = Policy.getPolicy();
 152         Policy p = new SimplePolicy(new RuntimePermission("setSecurityManager"),
 153                                     new PropertyPermission("line.separator", "read"));
 154         Policy.setPolicy(p);
 155         System.setSecurityManager(new SecurityManager());
 156         try {
 157             testLoadAndStore("UTF-8");
 158         } finally {
 159             // turn off security manager and restore policy
 160             System.setSecurityManager(null);
 161             Policy.setPolicy(orig);
 162         }
 163 
 164     }
 165 }