java-topology/test/jdk/com/sun/net/httpserver/TestLogging.java
russell@unturf.com 0a580b313d undefect. CWE-407 — 63 sites patched across 27 ecosystems
Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com

Patches, unit tests, benchmarks, whitepaper, and outreach briefs.
Public domain — no copyright claimed. Use freely.
2026-03-26 17:11:57 -04:00

128 lines
4.6 KiB
Java

/*
* Copyright (c) 2006, 2026, 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 com.sun.net.httpserver.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.*;
import java.util.logging.*;
import java.io.*;
import java.net.*;
import jdk.test.lib.net.URIBuilder;
import static jdk.test.lib.Utils.createTempFileOfSize;
/*
* @test
* @bug 6422914
* @summary change httpserver exception printouts
* @library /test/lib
* @build jdk.test.lib.Utils
* jdk.test.lib.net.URIBuilder
* @comment We use othervm because this test configures logging handlers
* for the system wide "com.sun.net.httpserver" logger
* @run main/othervm ${test.main.class}
* @run main/othervm -Djava.net.preferIPv6Addresses=true ${test.main.class}
*/
public class TestLogging extends Test {
private static final String TEMP_FILE_PREFIX =
HttpServer.class.getPackageName() + '-' + TestLogging.class.getSimpleName() + '-';
private static final Logger logger = Logger.getLogger("com.sun.net.httpserver");
private static void setupLogging() {
logger.setLevel(Level.ALL);
final Handler handler = new ConsoleHandler();
handler.setLevel(Level.ALL);
logger.addHandler(handler);
}
public static void main (String[] args) throws Exception {
HttpServer s1 = null;
ExecutorService executor=null;
Path filePath = createTempFileOfSize(TEMP_FILE_PREFIX, null, 0xBEEF);
try {
System.out.print ("Test9: ");
setupLogging();
String root = filePath.getParent().toString();
InetAddress loopback = InetAddress.getLoopbackAddress();
InetSocketAddress addr = new InetSocketAddress(loopback, 0);
s1 = HttpServer.create (addr, 0);
logger.info(root);
HttpHandler h = new FileServerHandler (root);
HttpContext c1 = s1.createContext ("/", h);
executor = Executors.newCachedThreadPool();
s1.setExecutor (executor);
s1.start();
int p1 = s1.getAddress().getPort();
URL url = URIBuilder.newBuilder()
.scheme("http")
.loopback()
.port(p1)
.path("/" + filePath.getFileName().toString())
.toURL();
System.out.println("URL: " + url);
HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
InputStream is = urlc.getInputStream();
while (is.read() != -1) ;
is.close();
url = URIBuilder.newBuilder()
.scheme("http")
.loopback()
.port(p1)
.path("/doesntexist.txt")
.toURLUnchecked();
System.out.println("URL: " + url);
urlc = (HttpURLConnection)url.openConnection();
try {
is = urlc.getInputStream();
while (is.read() != -1) ;
is.close();
} catch (IOException e) {
System.out.println ("caught expected exception");
}
Socket s = new Socket (InetAddress.getLoopbackAddress(), p1);
OutputStream os = s.getOutputStream();
//os.write ("GET xxx HTTP/1.1\r\n".getBytes());
os.write ("HELLO WORLD\r\n".getBytes());
is = s.getInputStream();
while (is.read() != -1) ;
os.close(); is.close(); s.close();
System.out.println ("OK");
} finally {
Files.delete(filePath);
if (s1 != null)
s1.stop(0);
if (executor != null)
executor.shutdown();
}
}
}