jdk/test/java/net/Socks/SocksProxyVersion.java
changeset 7982 65f5328a67a2
child 8160 4257e268578e
equal deleted inserted replaced
7981:2c27ff4c062e 7982:65f5328a67a2
       
     1 /*
       
     2  * Copyright (c) 2011, 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  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 6964547
       
    27  * @run main/othervm SocksProxyVersion
       
    28  * @summary test socksProxyVersion system property
       
    29  */
       
    30 
       
    31 import java.net.InetSocketAddress;
       
    32 import java.net.ServerSocket;
       
    33 import java.net.Socket;
       
    34 import java.net.SocketException;
       
    35 import java.io.DataInputStream;
       
    36 import java.io.IOException;
       
    37 
       
    38 public class SocksProxyVersion implements Runnable {
       
    39     ServerSocket ss;
       
    40     volatile boolean failed;
       
    41 
       
    42     public static void main(String[] args) throws Exception {
       
    43         new SocksProxyVersion();
       
    44     }
       
    45 
       
    46     public SocksProxyVersion() throws Exception {
       
    47         ss = new ServerSocket(0);
       
    48         int port = ss.getLocalPort();
       
    49         Thread serverThread = new Thread(this);
       
    50         serverThread.start();
       
    51 
       
    52         System.setProperty("socksProxyHost", "localhost");
       
    53         System.setProperty("socksProxyPort", Integer.toString(port));
       
    54 
       
    55         // SOCKS V4
       
    56         System.setProperty("socksProxyVersion", Integer.toString(4));
       
    57         try (Socket s = new Socket()) {
       
    58             s.connect(new InetSocketAddress("localhost", port));
       
    59         } catch (SocketException e) {
       
    60             // java.net.SocketException: Malformed reply from SOCKS server
       
    61             // This exception is OK, since the "server" does not implement
       
    62             // the socks protocol. It just verifies the version and closes.
       
    63         }
       
    64 
       
    65         // SOCKS V5
       
    66         System.setProperty("socksProxyVersion", Integer.toString(5));
       
    67         try (Socket s = new Socket()) {
       
    68             s.connect(new InetSocketAddress("localhost", port));
       
    69         } catch (SocketException e) { /* OK */ }
       
    70 
       
    71         serverThread.join();
       
    72         if (failed) {
       
    73             throw new RuntimeException("socksProxyVersion not being set correctly");
       
    74         }
       
    75     }
       
    76 
       
    77     public void run() {
       
    78         try (ss) {
       
    79             Socket s = ss.accept();
       
    80             int version = (s.getInputStream()).read();
       
    81             if (version != 4) {
       
    82                 System.out.println("Got " + version + ", expected 4");
       
    83                 failed = true;
       
    84             }
       
    85             s.close();
       
    86 
       
    87             s = ss.accept();
       
    88             version = (s.getInputStream()).read();
       
    89             if (version != 5) {
       
    90                 System.out.println("Got " + version + ", expected 5");
       
    91                 failed = true;
       
    92             }
       
    93             s.close();
       
    94         } catch (IOException e) {
       
    95             e.printStackTrace();
       
    96         }
       
    97     }
       
    98 }