jdk/test/java/net/URLPermission/nstest/LookupTest.java
changeset 21621 3101c2e7e705
parent 21344 13091b742137
child 21644 12edcc716b48
equal deleted inserted replaced
21620:5f7f2b541560 21621:3101c2e7e705
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    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
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /* @test
       
    25  * @compile -XDignore.symbol.file=true SimpleNameService.java
       
    26  *                                     SimpleNameServiceDescriptor.java
       
    27  * @run main/othervm/timeout=200 -Dsun.net.spi.nameservice.provider.1=simple,sun LookupTest
       
    28  */
       
    29 
       
    30 /**
    24 /**
    31  * This is a simple smoke test of the HttpURLPermission mechanism, which
    25  * This is a simple smoke test of the HttpURLPermission mechanism, which
    32  * checks for either IOException (due to unknown host) or SecurityException
    26  * checks for either IOException (due to unknown host) or SecurityException
    33  * due to lack of permission to connect
    27  * due to lack of permission to connect
    34  */
    28  */
    35 
    29 
    36 import java.net.*;
    30 import java.net.*;
    37 import java.io.*;
    31 import java.io.*;
       
    32 import jdk.testlibrary.Utils;
    38 
    33 
    39 public class LookupTest {
    34 public class LookupTest {
    40 
    35 
    41     static void test(
    36     static void test(
    42         String url, boolean throwsSecException, boolean throwsIOException)
    37         String url, boolean throwsSecException, boolean throwsIOException)
    46             System.err.println ("Connecting to " + u);
    41             System.err.println ("Connecting to " + u);
    47             URLConnection urlc = u.openConnection();
    42             URLConnection urlc = u.openConnection();
    48             InputStream is = urlc.getInputStream();
    43             InputStream is = urlc.getInputStream();
    49         } catch (SecurityException e) {
    44         } catch (SecurityException e) {
    50             if (!throwsSecException) {
    45             if (!throwsSecException) {
    51                 throw new RuntimeException ("(1) was not expecting " + e);
    46                 throw new RuntimeException ("(1) was not expecting ", e);
    52             }
    47             }
    53             return;
    48             return;
    54         } catch (IOException ioe) {
    49         } catch (IOException ioe) {
    55             if (!throwsIOException) {
    50             if (!throwsIOException) {
    56                 throw new RuntimeException ("(2) was not expecting " + ioe);
    51                 throw new RuntimeException ("(2) was not expecting ", ioe);
    57             }
    52             }
    58             return;
    53             return;
    59         }
    54         }
    60         if (throwsSecException || throwsIOException) {
    55         if (throwsSecException || throwsIOException) {
    61             System.err.printf ("was expecting a %s\n", throwsSecException ?
    56             System.err.printf ("was expecting a %s\n", throwsSecException ?
    62                 "security exception" : "IOException");
    57                 "security exception" : "IOException");
    63             throw new RuntimeException("was expecting an exception");
    58             throw new RuntimeException("was expecting an exception");
    64         }
    59         }
    65     }
    60     }
    66 
    61 
       
    62     static int port;
       
    63     static ServerSocket serverSocket;
       
    64 
    67     public static void main(String args[]) throws Exception {
    65     public static void main(String args[]) throws Exception {
    68         SimpleNameService.put("allowedAndFound.com", "127.0.0.1");
    66         String cmd = args[0];
    69         SimpleNameService.put("notAllowedButFound.com", "99.99.99.99");
    67         if (cmd.equals("-getport")) {
    70         // name "notAllowedAndNotFound.com" is not in map
    68             port = Utils.getFreePort();
    71         // name "allowedButNotfound.com" is not in map
    69             System.out.println(port);
    72         startServer();
    70         } else if (cmd.equals("-runtest")) {
       
    71             port = Integer.parseInt(args[1]);
       
    72             SimpleNameService.put("allowedAndFound.com", "127.0.0.1");
       
    73             SimpleNameService.put("notAllowedButFound.com", "99.99.99.99");
       
    74             // name "notAllowedAndNotFound.com" is not in map
       
    75             // name "allowedButNotfound.com" is not in map
       
    76             try {
       
    77                 startServer();
    73 
    78 
    74         String policyFileName = "file://" + System.getProperty("test.src", ".") + "/policy";
    79                 System.setSecurityManager(new SecurityManager());
    75         System.err.println ("policy = " + policyFileName);
       
    76 
    80 
    77         System.setProperty("java.security.policy", policyFileName);
    81                 test("http://allowedAndFound.com:" + port + "/foo", false, false);
    78 
    82 
    79         System.setSecurityManager(new SecurityManager());
    83                 test("http://notAllowedButFound.com:" + port + "/foo", true, false);
    80 
    84 
    81         test("http://allowedAndFound.com:50100/foo", false, false);
    85                 test("http://allowedButNotfound.com:" + port + "/foo", false, true);
    82 
    86 
    83         test("http://notAllowedButFound.com:50100/foo", true, false);
    87                 test("http://notAllowedAndNotFound.com:" + port + "/foo", true, false);
    84 
    88             } finally {
    85         test("http://allowedButNotfound.com:50100/foo", false, true);
    89                 serverSocket.close();
    86 
    90             }
    87         test("http://notAllowedAndNotFound.com:50100/foo", true, false);
    91         } else {
       
    92             throw new RuntimeException("Bad invocation: " + cmd);
       
    93         }
    88     }
    94     }
    89 
    95 
    90     static Thread server;
    96     static Thread server;
    91     static ServerSocket serverSocket;
       
    92 
    97 
    93     static class Server extends Thread {
    98     static class Server extends Thread {
    94         public void run() {
    99         public void run() {
    95             byte[] buf = new byte[1000];
   100             byte[] buf = new byte[1000];
    96             try {
   101             try {
   110             }
   115             }
   111     }
   116     }
   112 
   117 
   113     static void startServer() {
   118     static void startServer() {
   114         try {
   119         try {
   115             serverSocket = new ServerSocket(50100);
   120             serverSocket = new ServerSocket(port);
   116             server = new Server();
   121             server = new Server();
   117             server.start();
   122             server.start();
   118         } catch (Exception e) {
   123         } catch (Exception e) {
   119             throw new RuntimeException ("Test failed to initialize");
   124             throw new RuntimeException ("Test failed to initialize", e);
   120         }
   125         }
   121     }
   126     }
   122 }
   127 }