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