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 props.put("k3", "\\u0020\\u0391\\u0392\\u0393\\u0394\\u0395\\u0396\\u0397"); 79 props.put("k4", "\u7532\u9aa8\u6587"); 80 props.put("k5", "<java.home>/lib/jaxp.properties"); 81 82 ByteArrayOutputStream out = new ByteArrayOutputStream(); 83 props.storeToXML(out, null, encoding); 84 85 Properties p = new Properties(); 86 ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); 87 p.loadFromXML(in); 88 89 if (!p.equals(props)) { 90 System.err.println("stored: " + props); 91 System.err.println("loaded: " + p); 92 throw new RuntimeException("Test failed"); 93 } 94 } 95 96 /** 97 * Test loadFromXML with a document that does not have an encoding declaration 98 */ 99 static void testLoadWithoutEncoding() throws IOException { 100 System.out.println("testLoadWithoutEncoding"); 101 102 Properties expected = new Properties(); 103 expected.put("foo", "bar"); 104 105 String s = "<?xml version=\"1.0\"?>" + 106 "<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">" + 107 "<properties>" + 108 "<entry key=\"foo\">bar</entry>" + 109 "</properties>"; 110 ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes("UTF-8")); 111 Properties props = new Properties(); 112 props.loadFromXML(in); 113 114 if (!props.equals(expected)) { 115 System.err.println("loaded: " + props + ", expected: " + expected); 116 throw new RuntimeException("Test failed"); 117 } 118 } 119 120 /** 121 * Test loadFromXML with unsupported encoding 122 */ 123 static void testLoadWithBadEncoding() throws IOException { 124 System.out.println("testLoadWithBadEncoding"); 125 String s = "<?xml version=\"1.0\" encoding=\"BAD\"?>" + 126 "<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">" + 127 "<properties>" + 128 "<entry key=\"foo\">bar</entry>" + 129 "</properties>"; 130 ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes("UTF-8")); 131 Properties props = new Properties(); 132 try { 133 props.loadFromXML(in); 134 throw new RuntimeException("UnsupportedEncodingException expected"); 135 } catch (UnsupportedEncodingException expected) { } 136 } 137 138 /** 139 * Test storeToXML with unsupported encoding 140 */ 141 static void testStoreWithBadEncoding() throws IOException { 142 System.out.println("testStoreWithBadEncoding"); 143 Properties props = new Properties(); 144 props.put("foo", "bar"); 145 ByteArrayOutputStream out = new ByteArrayOutputStream(); 146 try { 147 props.storeToXML(out, null, "BAD"); 148 throw new RuntimeException("UnsupportedEncodingException expected"); 149 } catch (UnsupportedEncodingException expected) { } 150 } 151 152 /** 153 * Test loadFromXML with malformed documents 154 */ 155 static void testLoadWithMalformedDoc(Path dir) throws IOException { 156 try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.xml")) { 157 for (Path file: stream) { 158 System.out.println("testLoadWithMalformedDoc, file=" + file.getFileName()); 159 try (InputStream in = Files.newInputStream(file)) { 160 Properties props = new Properties(); 161 try { 162 props.loadFromXML(in); 163 throw new RuntimeException("InvalidPropertiesFormatException not thrown"); 164 } catch (InvalidPropertiesFormatException x) { 165 System.out.println(x); 166 } 167 } 168 } 169 } 170 } 171 172 public static void main(String[] args) throws IOException { 173 174 testLoadAndStore("UTF-8"); 175 testLoadAndStore("UTF-16"); 176 testLoadWithoutEncoding(); 177 testLoadWithBadEncoding(); 178 testStoreWithBadEncoding(); 179 180 // malformed documents 181 String src = System.getProperty("test.src"); 182 String subdir = "invalidxml"; 183 Path dir = (src == null) ? Paths.get(subdir) : Paths.get(src, subdir); 184 testLoadWithMalformedDoc(dir); 185 186 // re-run sanity test with security manager 187 Policy orig = Policy.getPolicy(); 188 Policy p = new SimplePolicy(new RuntimePermission("setSecurityManager"), 189 new PropertyPermission("line.separator", "read")); 190 Policy.setPolicy(p); 191 System.setSecurityManager(new SecurityManager()); 192 try { 193 testLoadAndStore("UTF-8"); 194 } finally { 195 // turn off security manager and restore policy 196 System.setSecurityManager(null); 197 Policy.setPolicy(orig); 198 } 199 200 } 201 }