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 8043119 27 * @summary Basic test of storeToXML and loadToXML 28 */ 29 30 import java.io.ByteArrayInputStream; 31 import java.io.ByteArrayOutputStream; 32 import java.io.IOException; 33 import java.io.InputStream; 34 import java.io.UnsupportedEncodingException; 35 import java.nio.file.DirectoryStream; 36 import java.nio.file.Files; 37 import java.nio.file.Path; 38 import java.nio.file.Paths; 39 import java.security.CodeSource; 40 import java.security.Permission; 41 import java.security.PermissionCollection; 42 import java.security.Permissions; 43 import java.security.Policy; 44 import java.security.ProtectionDomain; 45 import java.util.InvalidPropertiesFormatException; 46 import java.util.Properties; 47 import java.util.PropertyPermission; 48 49 public class LoadAndStoreXML { 50 51 /** 52 * Simple policy implementation that grants a set of permissions to 53 * all code sources and protection domains. 54 */ 55 static class SimplePolicy extends Policy { 56 private final Permissions perms; 57 58 public SimplePolicy(Permission...permissions) { 59 perms = new Permissions(); 60 for (Permission permission : permissions) 61 perms.add(permission); 62 } 63 64 @Override 65 public PermissionCollection getPermissions(CodeSource cs) { 66 return perms; 67 } 68 69 @Override 108 static class TestOutputStream extends ByteArrayOutputStream { 109 private boolean closed; 110 111 boolean isOpen() { 112 return !closed; 113 } 114 115 public void close() throws IOException { 116 try { 117 super.close(); 118 } finally { 119 closed = true; 120 } 121 } 122 } 123 124 /** 125 * Sanity test that properties saved with Properties#storeToXML can be 126 * read with Properties#loadFromXML. 127 */ 128 static void testLoadAndStore(String encoding) throws IOException { 129 System.out.println("testLoadAndStore, encoding=" + encoding); 130 131 Properties props = new Properties(); 132 props.put("k1", "foo"); 133 props.put("k2", "bar"); 134 props.put("k3", "\\u0020\\u0391\\u0392\\u0393\\u0394\\u0395\\u0396\\u0397"); 135 props.put("k4", "\u7532\u9aa8\u6587"); 136 props.put("k5", "<java.home>/lib/jaxp.properties"); 137 138 TestOutputStream out = new TestOutputStream(); 139 props.storeToXML(out, null, encoding); 140 if (!out.isOpen()) 141 throw new RuntimeException("OutputStream closed by storeToXML"); 142 143 Properties p = new Properties(); 144 TestInputStream in = new TestInputStream(out.toByteArray()); 145 p.loadFromXML(in); 146 if (in.isOpen()) 147 throw new RuntimeException("InputStream not closed by loadFromXML"); 148 149 if (!p.equals(props)) { 150 System.err.println("stored: " + props); 151 System.err.println("loaded: " + p); 152 throw new RuntimeException("Test failed"); 153 } 154 } 155 156 /** 157 * Test loadFromXML with a document that does not have an encoding declaration 158 */ 159 static void testLoadWithoutEncoding() throws IOException { 160 System.out.println("testLoadWithoutEncoding"); 161 162 Properties expected = new Properties(); 163 expected.put("foo", "bar"); 164 214 */ 215 static void testLoadWithMalformedDoc(Path dir) throws IOException { 216 try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.xml")) { 217 for (Path file: stream) { 218 System.out.println("testLoadWithMalformedDoc, file=" + file.getFileName()); 219 try (InputStream in = Files.newInputStream(file)) { 220 Properties props = new Properties(); 221 try { 222 props.loadFromXML(in); 223 throw new RuntimeException("InvalidPropertiesFormatException not thrown"); 224 } catch (InvalidPropertiesFormatException x) { 225 System.out.println(x); 226 } 227 } 228 } 229 } 230 } 231 232 public static void main(String[] args) throws IOException { 233 234 testLoadAndStore("UTF-8"); 235 testLoadAndStore("UTF-16"); 236 testLoadWithoutEncoding(); 237 testLoadWithBadEncoding(); 238 testStoreWithBadEncoding(); 239 240 // malformed documents 241 String src = System.getProperty("test.src"); 242 String subdir = "invalidxml"; 243 Path dir = (src == null) ? Paths.get(subdir) : Paths.get(src, subdir); 244 testLoadWithMalformedDoc(dir); 245 246 // re-run sanity test with security manager 247 Policy orig = Policy.getPolicy(); 248 Policy p = new SimplePolicy(new RuntimePermission("setSecurityManager"), 249 new PropertyPermission("line.separator", "read")); 250 Policy.setPolicy(p); 251 System.setSecurityManager(new SecurityManager()); 252 try { 253 testLoadAndStore("UTF-8"); 254 } finally { 255 // turn off security manager and restore policy 256 System.setSecurityManager(null); 257 Policy.setPolicy(orig); 258 } 259 260 } 261 } | 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 8043119 27 * @summary Basic test of storeToXML and loadToXML 28 */ 29 30 import java.io.ByteArrayInputStream; 31 import java.io.ByteArrayOutputStream; 32 import java.io.IOException; 33 import java.io.InputStream; 34 import java.io.UnsupportedEncodingException; 35 import java.nio.charset.Charset; 36 import java.nio.file.DirectoryStream; 37 import java.nio.file.Files; 38 import java.nio.file.Path; 39 import java.nio.file.Paths; 40 import java.security.CodeSource; 41 import java.security.Permission; 42 import java.security.PermissionCollection; 43 import java.security.Permissions; 44 import java.security.Policy; 45 import java.security.ProtectionDomain; 46 import java.util.InvalidPropertiesFormatException; 47 import java.util.Properties; 48 import java.util.PropertyPermission; 49 50 public class LoadAndStoreXML { 51 static final String bomChar = "\uFEFF"; 52 53 /** 54 * Simple policy implementation that grants a set of permissions to 55 * all code sources and protection domains. 56 */ 57 static class SimplePolicy extends Policy { 58 private final Permissions perms; 59 60 public SimplePolicy(Permission...permissions) { 61 perms = new Permissions(); 62 for (Permission permission : permissions) 63 perms.add(permission); 64 } 65 66 @Override 67 public PermissionCollection getPermissions(CodeSource cs) { 68 return perms; 69 } 70 71 @Override 110 static class TestOutputStream extends ByteArrayOutputStream { 111 private boolean closed; 112 113 boolean isOpen() { 114 return !closed; 115 } 116 117 public void close() throws IOException { 118 try { 119 super.close(); 120 } finally { 121 closed = true; 122 } 123 } 124 } 125 126 /** 127 * Sanity test that properties saved with Properties#storeToXML can be 128 * read with Properties#loadFromXML. 129 */ 130 static void testLoadAndStore(String encoding, boolean appendBOM) throws IOException { 131 System.out.println("testLoadAndStore, encoding=" + encoding); 132 133 Properties props = new Properties(); 134 props.put("k0", "\u6C34"); 135 props.put("k1", "foo"); 136 props.put("k2", "bar"); 137 props.put("k3", "\u0020\u0391\u0392\u0393\u0394\u0395\u0396\u0397"); 138 props.put("k4", "\u7532\u9aa8\u6587"); 139 props.put("k5", "<java.home>/lib/jaxp.properties"); 140 141 TestOutputStream out = new TestOutputStream(); 142 props.storeToXML(out, null, encoding); 143 if (!out.isOpen()) 144 throw new RuntimeException("OutputStream closed by storeToXML"); 145 146 Properties p = new Properties(); 147 TestInputStream in; 148 if (appendBOM) { 149 byte[] byteOrderMark = bomChar.getBytes(Charset.forName(encoding)); 150 byte[] outArray = out.toByteArray(); 151 byte[] inputArray = new byte[byteOrderMark.length + outArray.length]; 152 System.arraycopy(byteOrderMark, 0, inputArray, 0, byteOrderMark.length); 153 System.arraycopy(outArray, 0, inputArray, byteOrderMark.length, outArray.length); 154 in = new TestInputStream(inputArray); 155 } else { 156 in = new TestInputStream(out.toByteArray()); 157 } 158 p.loadFromXML(in); 159 if (in.isOpen()) 160 throw new RuntimeException("InputStream not closed by loadFromXML"); 161 162 if (!p.equals(props)) { 163 System.err.println("stored: " + props); 164 System.err.println("loaded: " + p); 165 throw new RuntimeException("Test failed"); 166 } 167 } 168 169 /** 170 * Test loadFromXML with a document that does not have an encoding declaration 171 */ 172 static void testLoadWithoutEncoding() throws IOException { 173 System.out.println("testLoadWithoutEncoding"); 174 175 Properties expected = new Properties(); 176 expected.put("foo", "bar"); 177 227 */ 228 static void testLoadWithMalformedDoc(Path dir) throws IOException { 229 try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.xml")) { 230 for (Path file: stream) { 231 System.out.println("testLoadWithMalformedDoc, file=" + file.getFileName()); 232 try (InputStream in = Files.newInputStream(file)) { 233 Properties props = new Properties(); 234 try { 235 props.loadFromXML(in); 236 throw new RuntimeException("InvalidPropertiesFormatException not thrown"); 237 } catch (InvalidPropertiesFormatException x) { 238 System.out.println(x); 239 } 240 } 241 } 242 } 243 } 244 245 public static void main(String[] args) throws IOException { 246 247 testLoadAndStore("UTF-8", false); 248 testLoadAndStore("UTF-16", false); 249 testLoadAndStore("UTF-16BE", false); 250 testLoadAndStore("UTF-16LE", false); 251 testLoadAndStore("UTF-16BE", true); 252 testLoadAndStore("UTF-16LE", true); 253 testLoadWithoutEncoding(); 254 testLoadWithBadEncoding(); 255 testStoreWithBadEncoding(); 256 257 // malformed documents 258 String src = System.getProperty("test.src"); 259 String subdir = "invalidxml"; 260 Path dir = (src == null) ? Paths.get(subdir) : Paths.get(src, subdir); 261 testLoadWithMalformedDoc(dir); 262 263 // re-run sanity test with security manager 264 Policy orig = Policy.getPolicy(); 265 Policy p = new SimplePolicy(new RuntimePermission("setSecurityManager"), 266 new PropertyPermission("line.separator", "read")); 267 Policy.setPolicy(p); 268 System.setSecurityManager(new SecurityManager()); 269 try { 270 testLoadAndStore("UTF-8", false); 271 } finally { 272 // turn off security manager and restore policy 273 System.setSecurityManager(null); 274 Policy.setPolicy(orig); 275 } 276 277 } 278 } |