Path: blob/master/test/jdk/java/net/Socks/SocksSocketImplTest.java
41149 views
/*1* Copyright (c) 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*/2223import org.testng.Assert;24import org.testng.annotations.AfterTest;25import org.testng.annotations.BeforeTest;26import org.testng.annotations.Test;27import sun.net.spi.DefaultProxySelector;2829import java.io.IOException;30import java.net.InetAddress;31import java.net.Proxy;32import java.net.ProxySelector;33import java.net.ServerSocket;34import java.net.Socket;35import java.net.URI;36import java.net.URISyntaxException;37import java.util.List;3839/**40* @test41* @bug 823031042* @summary Tests java.net.SocksSocketImpl43* @run testng SocksSocketImplTest44* @modules java.base/sun.net.spi:+open45*/46public class SocksSocketImplTest {4748private ProxySelector previousDefault;4950@BeforeTest51public void beforeTest() {52previousDefault = ProxySelector.getDefault();53ProxySelector.setDefault(new SchemeStrippedProxySelector());54}5556@AfterTest57public void afterTest() {58ProxySelector.setDefault(previousDefault);59}6061/**62* Creates a socket connection, which internally triggers proxy selection for the target63* address. The test has been configured to use a {@link SchemeStrippedProxySelector ProxySelector}64* which throws a {@link IllegalArgumentException}. This test then verifies that this IAE gets wrapped65* by {@code java.net.SocksSocketImpl} into an {@link IOException} before being thrown66*67* @throws Exception68*/69@Test70public void testIOEOnProxySelection() throws Exception {71final int backlog = -1;72final int port = 0;73try (ServerSocket ss = new ServerSocket(port, backlog, InetAddress.getLoopbackAddress());74Socket s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());75Socket s2 = ss.accept()) {76Assert.fail("IOException was expected to be thrown, but wasn't");77} catch (IOException ioe) {78// expected79// now verify the IOE was thrown for the correct expected reason80if (!(ioe.getCause() instanceof IllegalArgumentException)) {81// rethrow this so that the test output failure will capture the entire/real82// cause in its stacktrace83throw ioe;84}85}86}8788/**89* A {@link ProxySelector} which strips the "scheme" part of the {@link URI}90* before delegating the selection to the the {@link DefaultProxySelector}.91* This is to ensure that the {@code DefaultProxySelector} throws an {@link IllegalArgumentException}92* during selection of the proxy93*/94private static final class SchemeStrippedProxySelector extends DefaultProxySelector {9596@Override97public List<Proxy> select(final URI uri) {98System.out.println("Proxy selection for " + uri);99final URI schemeStrippedURI;100try {101// strip the scheme and pass the rest102schemeStrippedURI = new URI(null, uri.getHost(), uri.getPath(), null);103} catch (URISyntaxException e) {104throw new RuntimeException(e);105}106System.out.println("Scheme stripped URI " + schemeStrippedURI + " is being used to select a proxy");107return super.select(schemeStrippedURI);108}109}110}111112113