|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2001, 2017, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2001, 2025, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * This code is free software; you can redistribute it and/or modify it
|
|
25 | 25 | * @test
|
26 | 26 | * @bug 4401485
|
27 | 27 | * @requires (os.family == "windows")
|
| 28 | + * @modules java.base/sun.net.www.protocol.file |
| 29 | + * @library /test/lib |
28 | 30 | * @summary Check that URL.openConnection() doesn't open connection to UNC
|
29 |
| - * @run main UNCTest file://jdk/LOCAL-JAVA/jdk1.4/win/README.txt |
30 | 31 | */
|
31 | 32 |
|
| 33 | +import jtreg.SkippedException; |
| 34 | +import sun.net.www.protocol.file.FileURLConnection; |
| 35 | + |
| 36 | +import java.io.File; |
| 37 | +import java.net.InetAddress; |
| 38 | +import java.net.URI; |
32 | 39 | import java.net.URL;
|
33 | 40 | import java.net.URLConnection;
|
34 | 41 |
|
35 | 42 | public class UNCTest {
|
36 |
| - public static void main(String args[]) throws Exception { |
37 |
| - URL url = new URL( args[0] ); |
| 43 | + public static void main(String[] args) throws Exception { |
| 44 | + // Get the "computer name" for this host |
| 45 | + String hostName = InetAddress.getLocalHost().getHostName(); |
| 46 | + |
| 47 | + // UNC path which always exists with Administrative Shares enabled |
| 48 | + String path = "\\\\" + hostName + "\\C$\\Windows"; |
| 49 | + |
| 50 | + // Skip test if Administrative Shares is disabled |
| 51 | + if (! new File(path).exists()) { |
| 52 | + throw new SkippedException("Administrative Shares not enabled"); |
| 53 | + } |
| 54 | + |
| 55 | + // File URL for the UNC path |
| 56 | + URL url = new URI("file://" + hostName + "/C$/Windows").toURL(); |
| 57 | + |
| 58 | + // Should open a FileURLConnection for the UNC path |
38 | 59 | URLConnection conn = url.openConnection();
|
| 60 | + |
| 61 | + // Sanity check that the connection is a FileURLConnection |
| 62 | + if (! (conn instanceof FileURLConnection)) { |
| 63 | + throw new Exception("Expected FileURLConnection, instead got " |
| 64 | + + conn.getClass().getName()); |
| 65 | + } |
| 66 | + |
| 67 | + // Verify that the connection is not in an already connected state |
39 | 68 | conn.setRequestProperty( "User-Agent", "Java" );
|
40 | 69 | }
|
41 | 70 | }
|
0 commit comments