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 import java.nio.file.*;
  36 
  37 public class LoadAndStoreXML {
  38 
  39     /**
  40      * Simple policy implementation that grants a set of permissions to
  41      * all code sources and protection domains.
  42      */
  43     static class SimplePolicy extends Policy {
  44         private final Permissions perms;
  45 
  46         public SimplePolicy(Permission...permissions) {
  47             perms = new Permissions();
  48             for (Permission permission : permissions)
  49                 perms.add(permission);
  50         }
  51 
  52         @Override
  53         public PermissionCollection getPermissions(CodeSource cs) {
  54             return perms;
  55         }
  56 
  57         @Override
  58         public PermissionCollection getPermissions(ProtectionDomain pd) {
  59             return perms;
  60         }
  61 
  62         @Override
  63         public boolean implies(ProtectionDomain pd, Permission p) {
  64             return perms.implies(p);
  65         }
  66     }
  67 
  68     /**
  69      * Sanity test that properties saved with Properties#storeToXML can be
  70      * read with Properties#loadFromXML.
  71      */
  72     static void testLoadAndStore(String encoding) throws IOException {
  73         System.out.println("testLoadAndStore, encoding=" + encoding);
  74 
  75         Properties props = new Properties();
  76         props.put("k1", "foo");
  77         props.put("k2", "bar");
  78 
  79         ByteArrayOutputStream out = new ByteArrayOutputStream();
  80         props.storeToXML(out, null, encoding);
  81 
  82         Properties p = new Properties();
  83         ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
  84         p.loadFromXML(in);
  85 
  86         if (!p.equals(props)) {
  87             System.err.println("stored: " + props);
  88             System.err.println("loaded: " + p);
  89             throw new RuntimeException("Test failed");
  90         }
  91     }
  92 
  93     /**
  94      * Test loadFromXML with a document that does not have an encoding declaration
  95      */
  96     static void testLoadWithoutEncoding() throws IOException {
  97         System.out.println("testLoadWithoutEncoding");
  98 
  99         Properties expected = new Properties();
 100         expected.put("foo", "bar");
 101 
 102         String s = "<?xml version=\"1.0\"?>" +
 103                    "<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">" +
 104                    "<properties>" +
 105                    "<entry key=\"foo\">bar</entry>" +
 106                    "</properties>";
 107         ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes("UTF-8"));
 108         Properties props = new Properties();
 109         props.loadFromXML(in);
 110 
 111         if (!props.equals(expected)) {
 112             System.err.println("loaded: " + props + ", expected: " + expected);
 113             throw new RuntimeException("Test failed");
 114         }
 115     }
 116 
 117     /**
 118      * Test loadFromXML with unsupported encoding
 119      */
 120     static void testLoadWithBadEncoding() throws IOException {
 121         System.out.println("testLoadWithBadEncoding");
 122         String s = "<?xml version=\"1.0\" encoding=\"BAD\"?>" +
 123                    "<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">" +
 124                    "<properties>" +
 125                    "<entry key=\"foo\">bar</entry>" +
 126                    "</properties>";
 127         ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes("UTF-8"));
 128         Properties props = new Properties();
 129         try {
 130             props.loadFromXML(in);
 131             throw new RuntimeException("UnsupportedEncodingException expected");
 132         } catch (UnsupportedEncodingException expected) { }
 133     }
 134 
 135     /**
 136      * Test storeToXML with unsupported encoding
 137      */
 138     static void testStoreWithBadEncoding() throws IOException {
 139         System.out.println("testStoreWithBadEncoding");
 140         Properties props = new Properties();
 141         props.put("foo", "bar");
 142         ByteArrayOutputStream out = new ByteArrayOutputStream();
 143         try {
 144             props.storeToXML(out, null, "BAD");
 145             throw new RuntimeException("UnsupportedEncodingException expected");
 146         } catch (UnsupportedEncodingException expected) { }
 147     }
 148 
 149     /**
 150      * Test loadFromXML with malformed documents
 151      */
 152     static void testLoadWithMalformedDoc(Path dir) throws IOException {
 153         try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.xml")) {
 154             for (Path file: stream) {
 155                 System.out.println("testLoadWithMalformedDoc, file=" + file.getFileName());
 156                 try (InputStream in = Files.newInputStream(file)) {
 157                     Properties props = new Properties();
 158                     try {
 159                         props.loadFromXML(in);
 160                         throw new RuntimeException("InvalidPropertiesFormatException not thrown");
 161                     } catch (InvalidPropertiesFormatException x) {
 162                         System.out.println(x);
 163                     }
 164                 }
 165             }
 166         }
 167     }
 168 
 169     public static void main(String[] args) throws IOException {
 170 
 171         testLoadAndStore("UTF-8");
 172         testLoadAndStore("UTF-16");
 173         testLoadWithoutEncoding();
 174         testLoadWithBadEncoding();
 175         testStoreWithBadEncoding();
 176 
 177         // malformed documents
 178         String src = System.getProperty("test.src");
 179         String subdir = "invalidxml";
 180         Path dir = (src == null) ? Paths.get(subdir) : Paths.get(src, subdir);
 181         testLoadWithMalformedDoc(dir);
 182 
 183         // re-run sanity test with security manager
 184         Policy orig = Policy.getPolicy();
 185         Policy p = new SimplePolicy(new RuntimePermission("setSecurityManager"),
 186                                     new PropertyPermission("line.separator", "read"));
 187         Policy.setPolicy(p);
 188         System.setSecurityManager(new SecurityManager());
 189         try {
 190             testLoadAndStore("UTF-8");
 191         } finally {
 192             // turn off security manager and restore policy
 193             System.setSecurityManager(null);
 194             Policy.setPolicy(orig);
 195         }
 196 
 197     }
 198 }