2012-10-06 12:56:16 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
|
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @test
|
2012-12-19 14:53:33 +00:00
|
|
|
* @bug 8000354 8000685 8004371
|
2012-10-06 12:56:16 +00:00
|
|
|
* @summary Basic test of storeToXML and loadToXML
|
2012-12-19 14:53:33 +00:00
|
|
|
* @run main LoadAndStoreXML
|
|
|
|
* @run main/othervm -Dsun.util.spi.XmlPropertiesProvider=jdk.internal.util.xml.BasicXmlPropertiesProvider LoadAndStoreXML
|
2012-10-06 12:56:16 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
import java.util.*;
|
|
|
|
import java.security.*;
|
2012-12-19 14:53:33 +00:00
|
|
|
import java.nio.file.*;
|
2012-10-06 12:56:16 +00:00
|
|
|
|
|
|
|
public class LoadAndStoreXML {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simple policy implementation that grants a set of permissions to
|
|
|
|
* all code sources and protection domains.
|
|
|
|
*/
|
|
|
|
static class SimplePolicy extends Policy {
|
|
|
|
private final Permissions perms;
|
|
|
|
|
|
|
|
public SimplePolicy(Permission...permissions) {
|
|
|
|
perms = new Permissions();
|
|
|
|
for (Permission permission : permissions)
|
|
|
|
perms.add(permission);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public PermissionCollection getPermissions(CodeSource cs) {
|
|
|
|
return perms;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public PermissionCollection getPermissions(ProtectionDomain pd) {
|
|
|
|
return perms;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean implies(ProtectionDomain pd, Permission p) {
|
|
|
|
return perms.implies(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sanity test that properties saved with Properties#storeToXML can be
|
|
|
|
* read with Properties#loadFromXML.
|
|
|
|
*/
|
2012-10-17 10:43:56 +00:00
|
|
|
static void testLoadAndStore(String encoding) throws IOException {
|
2012-12-19 14:53:33 +00:00
|
|
|
System.out.println("testLoadAndStore, encoding=" + encoding);
|
|
|
|
|
2012-10-06 12:56:16 +00:00
|
|
|
Properties props = new Properties();
|
|
|
|
props.put("k1", "foo");
|
|
|
|
props.put("k2", "bar");
|
|
|
|
|
|
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
2012-10-17 10:43:56 +00:00
|
|
|
props.storeToXML(out, null, encoding);
|
2012-10-06 12:56:16 +00:00
|
|
|
|
|
|
|
Properties p = new Properties();
|
|
|
|
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
|
|
|
p.loadFromXML(in);
|
|
|
|
|
|
|
|
if (!p.equals(props)) {
|
|
|
|
System.err.println("stored: " + props);
|
|
|
|
System.err.println("loaded: " + p);
|
|
|
|
throw new RuntimeException("Test failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-17 10:43:56 +00:00
|
|
|
/**
|
|
|
|
* Test loadFromXML with a document that does not have an encoding declaration
|
|
|
|
*/
|
|
|
|
static void testLoadWithoutEncoding() throws IOException {
|
2012-12-19 14:53:33 +00:00
|
|
|
System.out.println("testLoadWithoutEncoding");
|
|
|
|
|
2012-10-17 10:43:56 +00:00
|
|
|
Properties expected = new Properties();
|
|
|
|
expected.put("foo", "bar");
|
|
|
|
|
|
|
|
String s = "<?xml version=\"1.0\"?>" +
|
|
|
|
"<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">" +
|
|
|
|
"<properties>" +
|
|
|
|
"<entry key=\"foo\">bar</entry>" +
|
|
|
|
"</properties>";
|
|
|
|
ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes("UTF-8"));
|
|
|
|
Properties props = new Properties();
|
|
|
|
props.loadFromXML(in);
|
|
|
|
|
|
|
|
if (!props.equals(expected)) {
|
|
|
|
System.err.println("loaded: " + props + ", expected: " + expected);
|
|
|
|
throw new RuntimeException("Test failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-19 14:53:33 +00:00
|
|
|
/**
|
|
|
|
* Test loadFromXML with unsupported encoding
|
|
|
|
*/
|
|
|
|
static void testLoadWithBadEncoding() throws IOException {
|
|
|
|
System.out.println("testLoadWithBadEncoding");
|
2012-10-17 10:43:56 +00:00
|
|
|
String s = "<?xml version=\"1.0\" encoding=\"BAD\"?>" +
|
|
|
|
"<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">" +
|
|
|
|
"<properties>" +
|
|
|
|
"<entry key=\"foo\">bar</entry>" +
|
|
|
|
"</properties>";
|
|
|
|
ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes("UTF-8"));
|
|
|
|
Properties props = new Properties();
|
|
|
|
try {
|
|
|
|
props.loadFromXML(in);
|
|
|
|
throw new RuntimeException("UnsupportedEncodingException expected");
|
|
|
|
} catch (UnsupportedEncodingException expected) { }
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test storeToXML with unsupported encoding
|
|
|
|
*/
|
|
|
|
static void testStoreWithBadEncoding() throws IOException {
|
2012-12-19 14:53:33 +00:00
|
|
|
System.out.println("testStoreWithBadEncoding");
|
2012-10-17 10:43:56 +00:00
|
|
|
Properties props = new Properties();
|
|
|
|
props.put("foo", "bar");
|
|
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
|
|
try {
|
|
|
|
props.storeToXML(out, null, "BAD");
|
|
|
|
throw new RuntimeException("UnsupportedEncodingException expected");
|
|
|
|
} catch (UnsupportedEncodingException expected) { }
|
|
|
|
}
|
|
|
|
|
2012-12-19 14:53:33 +00:00
|
|
|
/**
|
|
|
|
* Test loadFromXML with malformed documents
|
|
|
|
*/
|
|
|
|
static void testLoadWithMalformedDoc(Path dir) throws IOException {
|
|
|
|
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.xml")) {
|
|
|
|
for (Path file: stream) {
|
|
|
|
System.out.println("testLoadWithMalformedDoc, file=" + file.getFileName());
|
|
|
|
try (InputStream in = Files.newInputStream(file)) {
|
|
|
|
Properties props = new Properties();
|
|
|
|
try {
|
|
|
|
props.loadFromXML(in);
|
|
|
|
throw new RuntimeException("InvalidPropertiesFormatException not thrown");
|
|
|
|
} catch (InvalidPropertiesFormatException x) {
|
|
|
|
System.out.println(x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-06 12:56:16 +00:00
|
|
|
public static void main(String[] args) throws IOException {
|
|
|
|
|
2012-10-17 10:43:56 +00:00
|
|
|
testLoadAndStore("UTF-8");
|
|
|
|
testLoadAndStore("UTF-16");
|
|
|
|
testLoadWithoutEncoding();
|
|
|
|
testLoadWithBadEncoding();
|
|
|
|
testStoreWithBadEncoding();
|
2012-10-06 12:56:16 +00:00
|
|
|
|
2012-12-19 14:53:33 +00:00
|
|
|
// malformed documents
|
|
|
|
String src = System.getProperty("test.src");
|
|
|
|
String subdir = "invalidxml";
|
|
|
|
Path dir = (src == null) ? Paths.get(subdir) : Paths.get(src, subdir);
|
|
|
|
testLoadWithMalformedDoc(dir);
|
|
|
|
|
2012-10-17 10:43:56 +00:00
|
|
|
// re-run sanity test with security manager
|
2012-10-06 12:56:16 +00:00
|
|
|
Policy orig = Policy.getPolicy();
|
|
|
|
Policy p = new SimplePolicy(new RuntimePermission("setSecurityManager"),
|
|
|
|
new PropertyPermission("line.separator", "read"));
|
|
|
|
Policy.setPolicy(p);
|
|
|
|
System.setSecurityManager(new SecurityManager());
|
|
|
|
try {
|
2012-10-17 10:43:56 +00:00
|
|
|
testLoadAndStore("UTF-8");
|
2012-10-06 12:56:16 +00:00
|
|
|
} finally {
|
|
|
|
// turn off security manager and restore policy
|
|
|
|
System.setSecurityManager(null);
|
|
|
|
Policy.setPolicy(orig);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|