Path: blob/master/test/jdk/java/net/Socks/SocksV4Test.java
41149 views
/*1* Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 472754726* @summary SocksSocketImpl throws NullPointerException27* @build SocksServer28* @run main/othervm SocksV4Test29*/3031import java.io.IOException;32import java.net.Authenticator;33import java.net.InetAddress;34import java.net.InetSocketAddress;35import java.net.PasswordAuthentication;36import java.net.Proxy;37import java.net.ServerSocket;38import java.net.Socket;39import java.net.UnknownHostException;4041public class SocksV4Test {4243// An unresolvable host44static final String HOSTNAME = "doesnot.exist.invalid";45static final String USER = "johndoe";46static final String PASSWORD = "helloworld";4748public static void main(String[] args) throws Exception {49Authenticator.setDefault(new Auth());50UHETest();51getLocalPortTest();52}5354static class Auth extends Authenticator {55protected PasswordAuthentication getPasswordAuthentication() {56return new PasswordAuthentication(USER, PASSWORD.toCharArray());57}58}5960public static void getLocalPortTest() throws Exception {61// We actually use V5 for this test because that is the default62// protocol version used by the client and it doesn't really handle63// down grading very well.64InetAddress lba = InetAddress.getLoopbackAddress();65try (SocksServer srvr = new SocksServer(lba, 0, false);66ServerSocket ss = new ServerSocket(0, 0, lba)) {6768srvr.addUser(USER, PASSWORD);69int serverPort = ss.getLocalPort();70srvr.start();71int proxyPort = srvr.getPort();72System.out.printf("Server port %d, Proxy port %d\n", serverPort, proxyPort);73Proxy sp = new Proxy(Proxy.Type.SOCKS,74new InetSocketAddress(lba, proxyPort));75// Let's create an unresolved address76InetSocketAddress ad = new InetSocketAddress(lba.getHostAddress(), serverPort);77try (Socket s = new Socket(sp)) {78s.connect(ad, 10000);79int pp = s.getLocalPort();80System.out.println("Local port = " + pp);81if (pp == serverPort || pp == proxyPort)82throw new RuntimeException("wrong port returned");83} catch (UnknownHostException ex) {84throw new RuntimeException(ex);85} catch (IOException ioe) {86throw new RuntimeException(ioe);87}88}89}9091public static void UHETest() throws Exception {92// sanity before running the test93assertUnresolvableHost(HOSTNAME);9495InetAddress lba = InetAddress.getLoopbackAddress();96// Create a SOCKS V4 proxy97try (SocksServer srvr = new SocksServer(lba, 0, true)) {98srvr.start();99Proxy sp = new Proxy(Proxy.Type.SOCKS,100new InetSocketAddress(lba, srvr.getPort()));101// Let's create an unresolved address102InetSocketAddress ad = new InetSocketAddress(HOSTNAME, 1234);103try (Socket s = new Socket(sp)) {104s.connect(ad, 10000);105} catch (UnknownHostException ex) {106// OK, that's what we expected107} catch (NullPointerException npe) {108// Not OK, this used to be the bug109throw new RuntimeException("Got a NUllPointerException");110}111}112}113114static void assertUnresolvableHost(String host) {115InetAddress addr = null;116try {117addr = InetAddress.getByName(host);118} catch (UnknownHostException x) {119// OK, expected120}121if (addr != null)122throw new RuntimeException("Test cannot run. resolvable address:" + addr);123}124}125126127