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