author | asmotrak |
Fri, 03 Jul 2015 08:00:33 +0100 | |
changeset 31529 | 31d7d82b39ff |
parent 23010 | 6dadb192ad81 |
permissions | -rw-r--r-- |
7982 | 1 |
/* |
31529
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
2 |
* Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved. |
7982 | 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 |
|
31529
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
26 |
* @bug 6964547 5001942 8129444 |
7982 | 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.IOException; |
|
16737
8f36190e097a
5001942: Missings SOCKS support for direct connections
khazra
parents:
8160
diff
changeset
|
36 |
import java.net.InetAddress; |
31529
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
37 |
import java.net.Proxy; |
7982 | 38 |
|
39 |
public class SocksProxyVersion implements Runnable { |
|
8160
4257e268578e
7015410: test/java/net/Socks/SocksProxyVersion.java needs to be updated due to 7013420
alanb
parents:
7982
diff
changeset
|
40 |
final ServerSocket ss; |
7982 | 41 |
volatile boolean failed; |
31529
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
42 |
volatile boolean stopped = false; |
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
43 |
volatile int expected; |
7982 | 44 |
|
45 |
public static void main(String[] args) throws Exception { |
|
20783
c0806cb8d4ae
8023555: test/java/net/Socks/SocksProxyVersion.java fails when machine name is localhost
tyan
parents:
16737
diff
changeset
|
46 |
if (InetAddress.getLocalHost().isLoopbackAddress()) { |
c0806cb8d4ae
8023555: test/java/net/Socks/SocksProxyVersion.java fails when machine name is localhost
tyan
parents:
16737
diff
changeset
|
47 |
System.out.println("Test cannot run. getLocalHost returns a loopback address"); |
c0806cb8d4ae
8023555: test/java/net/Socks/SocksProxyVersion.java fails when machine name is localhost
tyan
parents:
16737
diff
changeset
|
48 |
return; |
c0806cb8d4ae
8023555: test/java/net/Socks/SocksProxyVersion.java fails when machine name is localhost
tyan
parents:
16737
diff
changeset
|
49 |
} |
7982 | 50 |
new SocksProxyVersion(); |
51 |
} |
|
52 |
||
53 |
public SocksProxyVersion() throws Exception { |
|
54 |
ss = new ServerSocket(0); |
|
31529
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
55 |
int port = ss.getLocalPort(); |
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
56 |
Thread serverThread = new Thread(this); |
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
57 |
serverThread.start(); |
8160
4257e268578e
7015410: test/java/net/Socks/SocksProxyVersion.java needs to be updated due to 7013420
alanb
parents:
7982
diff
changeset
|
58 |
try (ServerSocket socket = ss) { |
31529
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
59 |
runTest(port); |
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
60 |
} finally { |
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
61 |
stopped = true; |
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
62 |
} |
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
63 |
|
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
64 |
serverThread.join(); |
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
65 |
if (failed) { |
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
66 |
throw new RuntimeException("socksProxyVersion not being set correctly"); |
8160
4257e268578e
7015410: test/java/net/Socks/SocksProxyVersion.java needs to be updated due to 7013420
alanb
parents:
7982
diff
changeset
|
67 |
} |
4257e268578e
7015410: test/java/net/Socks/SocksProxyVersion.java needs to be updated due to 7013420
alanb
parents:
7982
diff
changeset
|
68 |
} |
4257e268578e
7015410: test/java/net/Socks/SocksProxyVersion.java needs to be updated due to 7013420
alanb
parents:
7982
diff
changeset
|
69 |
|
31529
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
70 |
final void runTest(int port) throws Exception { |
16737
8f36190e097a
5001942: Missings SOCKS support for direct connections
khazra
parents:
8160
diff
changeset
|
71 |
/* |
31529
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
72 |
* Retrieving the IP Address of the machine |
16737
8f36190e097a
5001942: Missings SOCKS support for direct connections
khazra
parents:
8160
diff
changeset
|
73 |
* since "localhost" is bypassed as a non-proxy host |
8f36190e097a
5001942: Missings SOCKS support for direct connections
khazra
parents:
8160
diff
changeset
|
74 |
*/ |
8f36190e097a
5001942: Missings SOCKS support for direct connections
khazra
parents:
8160
diff
changeset
|
75 |
String addr = InetAddress.getLocalHost().getHostAddress(); |
8f36190e097a
5001942: Missings SOCKS support for direct connections
khazra
parents:
8160
diff
changeset
|
76 |
|
8f36190e097a
5001942: Missings SOCKS support for direct connections
khazra
parents:
8160
diff
changeset
|
77 |
System.setProperty("socksProxyHost", addr); |
7982 | 78 |
System.setProperty("socksProxyPort", Integer.toString(port)); |
79 |
||
31529
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
80 |
Proxy proxy = new Proxy(Proxy.Type.SOCKS, |
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
81 |
new InetSocketAddress(addr, port)); |
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
82 |
|
7982 | 83 |
// SOCKS V4 |
84 |
System.setProperty("socksProxyVersion", Integer.toString(4)); |
|
31529
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
85 |
this.expected = 4; |
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
86 |
check(new Socket(), addr, port); |
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
87 |
check(new Socket(proxy), addr, port); |
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
88 |
|
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
89 |
// SOCKS V5 |
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
90 |
System.setProperty("socksProxyVersion", Integer.toString(5)); |
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
91 |
this.expected = 5; |
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
92 |
check(new Socket(), addr, port); |
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
93 |
check(new Socket(proxy), addr, port); |
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
94 |
} |
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
95 |
|
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
96 |
private void check(Socket socket, String addr, int port) |
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
97 |
throws IOException |
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
98 |
{ |
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
99 |
try (Socket s = socket) { |
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
100 |
socket.connect(new InetSocketAddress(addr, port)); |
7982 | 101 |
} catch (SocketException e) { |
102 |
// java.net.SocketException: Malformed reply from SOCKS server |
|
103 |
// This exception is OK, since the "server" does not implement |
|
104 |
// the socks protocol. It just verifies the version and closes. |
|
105 |
} |
|
106 |
} |
|
107 |
||
31529
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
108 |
@Override |
7982 | 109 |
public void run() { |
31529
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
110 |
int count = 0; |
8160
4257e268578e
7015410: test/java/net/Socks/SocksProxyVersion.java needs to be updated due to 7013420
alanb
parents:
7982
diff
changeset
|
111 |
try { |
31529
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
112 |
while (!stopped) { |
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
113 |
try (Socket s = ss.accept()) { |
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
114 |
int version = (s.getInputStream()).read(); |
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
115 |
if (version != expected) { |
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
116 |
System.out.printf("Iteration: %d, Got: %d, expected: %d%n", |
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
117 |
count, version, expected); |
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
118 |
failed = true; |
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
119 |
} |
8160
4257e268578e
7015410: test/java/net/Socks/SocksProxyVersion.java needs to be updated due to 7013420
alanb
parents:
7982
diff
changeset
|
120 |
} |
31529
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
121 |
count++; |
7982 | 122 |
} |
123 |
} catch (IOException e) { |
|
31529
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
124 |
if (!ss.isClosed()) { |
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
125 |
e.printStackTrace(); |
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
126 |
} |
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
23010
diff
changeset
|
127 |
// ignore, server socket was closed |
7982 | 128 |
} |
129 |
} |
|
130 |
} |