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
  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 test() 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, "no comment");
  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     public static void main(String[] args) throws IOException {
  89 
  90         // run test without security manager
  91         test();
  92 
  93         // re-run test with security manager
  94         Policy orig = Policy.getPolicy();
  95         Policy p = new SimplePolicy(new RuntimePermission("setSecurityManager"),
  96                                     new PropertyPermission("line.separator", "read"));
  97         Policy.setPolicy(p);
  98         System.setSecurityManager(new SecurityManager());
  99         try {
 100             test();
 101         } finally {
 102             // turn off security manager and restore policy
 103             System.setSecurityManager(null);
 104             Policy.setPolicy(orig);
 105         }
 106 
 107     }
 108 }