jdk/test/java/net/Socks/BadProxySelector.java
author coffeys
Wed, 25 Feb 2015 11:44:53 +0000
changeset 29112 df7e4edb6566
permissions -rw-r--r--
7178362: Socket impls should ignore unsupported proxy types rather than throwing Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29112
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
     1
/*
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
     4
 *
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
     7
 * published by the Free Software Foundation.
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
     8
 *
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    13
 * accompanied this code).
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    14
 *
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    18
 *
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    21
 * questions.
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    22
 */
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    23
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    24
/*
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    25
 * @test
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    26
 * @bug 7178362
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    27
 * @run main/othervm BadProxySelector
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    28
 */
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    29
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    30
import java.net.InetSocketAddress;
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    31
import java.net.Proxy;
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    32
import java.net.ProxySelector;
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    33
import java.net.Socket;
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    34
import java.net.SocketAddress;
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    35
import java.net.ServerSocket;
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    36
import java.net.URI;
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    37
import java.util.ArrayList;
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    38
import java.util.List;
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    39
import java.io.*;
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    40
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    41
public class BadProxySelector {
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    42
    public static void main(String[] args) throws Exception {
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    43
        ProxySelector.setDefault(new HTTPProxySelector());
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    44
        try (ServerSocket ss = new ServerSocket(0);
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    45
             Socket s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    46
             Socket s2 = ss.accept()) {
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    47
        }
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    48
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    49
       ProxySelector.setDefault(new NullHTTPProxySelector());
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    50
        try (ServerSocket ss = new ServerSocket(0);
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    51
             Socket s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    52
             Socket s2 = ss.accept()) {
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    53
        }
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    54
    }
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    55
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    56
    // always returns bogus HTTP proxies
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    57
    private static class HTTPProxySelector extends ProxySelector {
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    58
        @Override
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    59
        public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {}
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    60
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    61
        @Override
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    62
        public List<Proxy> select(URI uri) {
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    63
            List<Proxy> proxies = new ArrayList<>();
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    64
            proxies.add(new Proxy(Proxy.Type.HTTP,
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    65
                                  new InetSocketAddress("localhost", 0)));
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    66
            proxies.add(new Proxy(Proxy.Type.HTTP,
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    67
                                  new InetSocketAddress("localhost", 0)));
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    68
            return proxies;
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    69
        }
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    70
    }
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    71
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    72
    private static class NullHTTPProxySelector extends ProxySelector {
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    73
        @Override
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    74
        public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {}
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    75
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    76
        @Override
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    77
        public List<Proxy> select(URI uri) {
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    78
            List<Proxy> proxies = new ArrayList<>();
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    79
            proxies.add(null);
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    80
            proxies.add(new Proxy(Proxy.Type.HTTP,
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    81
                                  new InetSocketAddress("localhost", 0)));
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    82
            return proxies;
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    83
        }
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    84
    }
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
diff changeset
    85
}