1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
/*
* Copyright (c) 2015, 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.
*/
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Properties;
import java.util.logging.LogManager;
/**
* @test
* @bug 8075810
* @run main/othervm InvalidEscapeConfigurationTest
* @author danielfuchs
*/
public class InvalidEscapeConfigurationTest {
public static void main(String[] args)
throws UnsupportedEncodingException, IOException {
String[] validEscapes = {
"com.f\\u006fo.level = INF\\u004f",
"com.f\\u006fo.level = INFO",
"com.foo.level = INF\\u004f"
};
String[] invalidEscapes = {
"com.fo\\u0O6f.level = INF\\u0O4f",
"com.fo\\u0O6f.level = INFO",
"com.foo.level = INF\\u0O4f"
};
for (String line : validEscapes) {
test(line, true);
}
for (String line : invalidEscapes) {
test(line, false);
}
try {
Properties props = new Properties();
props.load((InputStream)null);
throw new RuntimeException("Properties.load(null): "
+ "NullPointerException exception not raised");
} catch (NullPointerException x) {
System.out.println("Properties.load(null): "
+ "got expected exception: " + x);
}
try {
LogManager.getLogManager().readConfiguration(null);
throw new RuntimeException("LogManager.readConfiguration(null): "
+ "NullPointerException exception not raised");
} catch (NullPointerException x) {
System.out.println("LogManager.readConfiguration(null): "
+ "got expected exception: " + x);
}
}
public static void test(String line, boolean valid) throws IOException {
String test = (valid ? "valid" : "invalid")
+ " line \"" +line + "\"";
System.out.println("Testing " + test);
// First verify that we get the expected result from Properties.load()
try {
ByteArrayInputStream bais =
new ByteArrayInputStream(line.getBytes("UTF-8"));
Properties props = new Properties();
props.load(bais);
if (!valid) {
throw new RuntimeException(test
+ "\n\tProperties.load: expected exception not raised");
} else {
System.out.println("Properties.load passed for " + test);
}
} catch(IllegalArgumentException x) {
if (!valid) {
System.out.println(
"Properties.load: Got expected exception: "
+ x + "\n\tfor " + test);
} else {
throw x;
}
}
// Then verify that we get the expected result from
// LogManager.readConfiguration
try {
String content = defaultConfiguration() + '\n' + line + '\n';
ByteArrayInputStream bais =
new ByteArrayInputStream(content.getBytes("UTF-8"));
LogManager.getLogManager().readConfiguration(bais);
if (!valid) {
throw new RuntimeException(test
+ "\n\tLogManager.readConfiguration: "
+ "expected exception not raised");
} else {
System.out.println("LogManager.readConfiguration passed for "
+ test);
}
} catch(IOException x) {
if (!valid) {
System.out.println(
"LogManager.readConfiguration: Got expected exception: "
+ x + "\n\tfor " + test);
} else {
throw x;
}
}
}
static String getConfigurationFileName() {
String fname = System.getProperty("java.util.logging.config.file");
if (fname == null) {
fname = System.getProperty("java.home");
if (fname == null) {
throw new Error("Can't find java.home ??");
}
fname = Paths.get(fname, "conf", "logging.properties")
.toAbsolutePath().normalize().toString();
}
return fname;
}
static String defaultConfiguration() throws IOException {
Properties props = new Properties();
String fileName = getConfigurationFileName();
if (Files.exists(Paths.get(fileName))) {
try (InputStream is = new FileInputStream(fileName);) {
props.load(is);
} catch(IOException x) {
throw new UncheckedIOException(x);
}
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
props.store(bos, null);
return bos.toString();
}
}
|