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