Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/SocketImpl/SocketImplCombinations.java
41152 views
1
/*
2
* Copyright (c) 2019, 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 8220493
27
* @modules java.base/java.net:+open java.base/sun.nio.ch:+open
28
* @run testng/othervm SocketImplCombinations
29
* @summary Test Socket and ServerSocket with combinations of SocketImpls
30
*/
31
32
import java.io.FileDescriptor;
33
import java.io.IOException;
34
import java.io.InputStream;
35
import java.io.OutputStream;
36
import java.lang.reflect.Field;
37
import java.net.InetAddress;
38
import java.net.InetSocketAddress;
39
import java.net.Proxy;
40
import java.net.ServerSocket;
41
import java.net.Socket;
42
import java.net.SocketAddress;
43
import java.net.SocketImpl;
44
import java.net.SocketImplFactory;
45
import java.nio.channels.ServerSocketChannel;
46
import java.nio.channels.SocketChannel;
47
import java.util.function.BiConsumer;
48
49
import org.testng.annotations.Test;
50
import static org.testng.Assert.*;
51
52
@Test
53
public class SocketImplCombinations {
54
55
/**
56
* Test creating an unconnected Socket, it should be created with a platform SocketImpl.
57
*/
58
public void testNewSocket1() throws IOException {
59
try (Socket s = new Socket()) {
60
SocketImpl si = getSocketImpl(s);
61
assertTrue(isSocksSocketImpl(si));
62
SocketImpl delegate = getDelegate(si);
63
assertTrue(isPlatformSocketImpl(delegate));
64
}
65
}
66
67
/**
68
* Test creating a connected Socket, it should be created with a platform SocketImpl.
69
*/
70
public void testNewSocket2() throws IOException {
71
try (ServerSocket ss = boundServerSocket()) {
72
try (Socket s = new Socket(ss.getInetAddress(), ss.getLocalPort())) {
73
SocketImpl si = getSocketImpl(s);
74
assertTrue(isSocksSocketImpl(si));
75
SocketImpl delegate = getDelegate(si);
76
assertTrue(isPlatformSocketImpl(delegate));
77
}
78
}
79
}
80
81
/**
82
* Test creating a Socket for a DIRECT connection, it should be created with a
83
* platform SocketImpl.
84
*/
85
public void testNewSocket3() throws IOException {
86
try (Socket s = new Socket(Proxy.NO_PROXY)) {
87
SocketImpl si = getSocketImpl(s);
88
assertTrue(isPlatformSocketImpl(si));
89
}
90
}
91
92
/**
93
* Test creating a Socket for a SOCKS connection, it should be created with a
94
* SOCKS SocketImpl.
95
*/
96
public void testNewSocket4() throws IOException {
97
var address = new InetSocketAddress("127.0.0.1", 1080);
98
var socksProxy = new Proxy(Proxy.Type.SOCKS, address);
99
try (Socket s = new Socket(socksProxy)) {
100
SocketImpl si = getSocketImpl(s);
101
assertTrue(isSocksSocketImpl(si));
102
SocketImpl delegate = getDelegate(si);
103
assertTrue(isPlatformSocketImpl(delegate));
104
}
105
}
106
107
/**
108
* Test creating a Socket for a HTTP proxy connection, it should be created with
109
* a HTTP proxy SocketImpl.
110
*/
111
public void testNewSocket5() throws IOException {
112
var address = new InetSocketAddress("127.0.0.1", 8080);
113
var httpProxy = new Proxy(Proxy.Type.HTTP, address);
114
try (Socket s = new Socket(httpProxy)) {
115
SocketImpl si = getSocketImpl(s);
116
assertTrue(isHttpConnectSocketImpl(si));
117
SocketImpl delegate = getDelegate(si);
118
assertTrue(isPlatformSocketImpl(delegate));
119
}
120
}
121
122
/**
123
* Test creating a Socket no SocketImpl. A platform SocketImpl should be
124
* created lazily.
125
*/
126
public void testNewSocket6() throws IOException {
127
Socket s = new Socket((SocketImpl) null) { };
128
try (s) {
129
assertTrue(getSocketImpl(s) == null);
130
s.bind(loopbackSocketAddress()); // force SocketImpl to be created
131
SocketImpl si = getSocketImpl(s);
132
assertTrue(isSocksSocketImpl(si));
133
SocketImpl delegate = getDelegate(si);
134
assertTrue(isPlatformSocketImpl(delegate));
135
}
136
}
137
138
/**
139
* Test creating a Socket with a custom SocketImpl.
140
*/
141
public void testNewSocket7() throws IOException {
142
Socket s = new Socket(new CustomSocketImpl(false)) { };
143
try (s) {
144
SocketImpl si = getSocketImpl(s);
145
assertTrue(si instanceof CustomSocketImpl);
146
}
147
}
148
149
/**
150
* Test creating a Socket when there is a SocketImplFactory set.
151
*/
152
public void testNewSocket8() throws IOException {
153
setSocketSocketImplFactory(() -> new CustomSocketImpl(false));
154
try (Socket s = new Socket()) {
155
SocketImpl si = getSocketImpl(s);
156
assertTrue(si instanceof CustomSocketImpl);
157
} finally {
158
setSocketSocketImplFactory(null);
159
}
160
}
161
162
/**
163
* Test creating a Socket for a DIRECT connection when there is a
164
* SocketImplFactory set.
165
*/
166
public void testNewSocket9() throws IOException {
167
setSocketSocketImplFactory(() -> new CustomSocketImpl(false));
168
try (Socket s = new Socket(Proxy.NO_PROXY)) {
169
SocketImpl si = getSocketImpl(s);
170
assertTrue(si instanceof CustomSocketImpl);
171
} finally {
172
setSocketSocketImplFactory(null);
173
}
174
}
175
176
/**
177
* Test creating a Socket for a SOCKS connection when there is a
178
* SocketImplFactory set.
179
*/
180
public void testNewSocket10() throws IOException {
181
var address = new InetSocketAddress("127.0.0.1", 1080);
182
var socksProxy = new Proxy(Proxy.Type.SOCKS, address);
183
setSocketSocketImplFactory(() -> new CustomSocketImpl(false));
184
try (Socket s = new Socket(socksProxy)) {
185
SocketImpl si = getSocketImpl(s);
186
assertTrue(isSocksSocketImpl(si));
187
SocketImpl delegate = getDelegate(si);
188
assertTrue(isPlatformSocketImpl(delegate));
189
} finally {
190
setSocketSocketImplFactory(null);
191
}
192
}
193
194
/**
195
* Test creating a Socket for a HTTP proxy connection when there is a
196
* SocketImplFactory set.
197
*/
198
public void testNewSocket11() throws IOException {
199
var address = new InetSocketAddress("127.0.0.1", 8080);
200
var httpProxy = new Proxy(Proxy.Type.HTTP, address);
201
setSocketSocketImplFactory(() -> new CustomSocketImpl(false));
202
try (Socket s = new Socket(httpProxy)) {
203
SocketImpl si = getSocketImpl(s);
204
assertTrue(isHttpConnectSocketImpl(si));
205
SocketImpl delegate = getDelegate(si);
206
assertTrue(isPlatformSocketImpl(delegate));
207
} finally {
208
setSocketSocketImplFactory(null);
209
}
210
}
211
212
/**
213
* Test creating a Socket no SocketImpl when there is a SocketImplFactory set.
214
*/
215
public void testNewSocket12() throws IOException {
216
setSocketSocketImplFactory(() -> new CustomSocketImpl(false));
217
try {
218
Socket s = new Socket((SocketImpl) null) { };
219
try (s) {
220
assertTrue(getSocketImpl(s) == null);
221
s.bind(loopbackSocketAddress()); // force SocketImpl to be created
222
assertTrue(getSocketImpl(s) instanceof CustomSocketImpl);
223
}
224
} finally {
225
setSocketSocketImplFactory(null);
226
}
227
}
228
229
/**
230
* Test creating an unbound ServerSocket, it should be created with a platform
231
* SocketImpl.
232
*/
233
public void testNewServerSocket1() throws IOException {
234
try (ServerSocket ss = new ServerSocket()) {
235
SocketImpl si = getSocketImpl(ss);
236
assertTrue(isPlatformSocketImpl(si));
237
}
238
}
239
240
/**
241
* Test creating a bound ServerSocket, it should be created with a platform
242
* SocketImpl.
243
*/
244
public void testNewServerSocket2() throws IOException {
245
try (ServerSocket ss = new ServerSocket(0)) {
246
SocketImpl si = getSocketImpl(ss);
247
assertTrue(isPlatformSocketImpl(si));
248
}
249
}
250
251
/**
252
* Test creating a ServerSocket with a custom SocketImpl.
253
*/
254
public void testNewServerSocket3() throws IOException {
255
ServerSocket ss = new ServerSocket(new CustomSocketImpl(true)) { };
256
try (ss) {
257
SocketImpl si = getSocketImpl(ss);
258
assertTrue(si instanceof CustomSocketImpl);
259
}
260
}
261
262
/**
263
* Test creating an unbound ServerSocket when there is a SocketImplFactory set.
264
*/
265
public void testNewServerSocket4() throws IOException {
266
setServerSocketImplFactory(() -> new CustomSocketImpl(true));
267
try (ServerSocket ss = new ServerSocket()) {
268
SocketImpl si = getSocketImpl(ss);
269
assertTrue(si instanceof CustomSocketImpl);
270
} finally {
271
setServerSocketImplFactory(null);
272
}
273
}
274
275
/**
276
* Test creating a bound ServerSocket when there is a SocketImplFactory set.
277
*/
278
public void testNewServerSocket5() throws IOException {
279
setServerSocketImplFactory(() -> new CustomSocketImpl(true));
280
try (ServerSocket ss = new ServerSocket(0)) {
281
SocketImpl si = getSocketImpl(ss);
282
assertTrue(si instanceof CustomSocketImpl);
283
} finally {
284
setServerSocketImplFactory(null);
285
}
286
}
287
288
/**
289
* Test ServerSocket.accept. The ServerSocket uses a platform SocketImpl,
290
* the Socket to accept is created with no SocketImpl.
291
*/
292
public void testServerSocketAccept1() throws IOException {
293
var socket = new Socket((SocketImpl) null) { };
294
assertTrue(getSocketImpl(socket) == null);
295
296
serverSocketAccept(socket, (ss, s) -> {
297
assertTrue(isPlatformSocketImpl(getSocketImpl(ss)));
298
assertTrue(s == socket);
299
SocketImpl si = getSocketImpl(s);
300
assertTrue(isPlatformSocketImpl(si));
301
checkFields(si);
302
});
303
}
304
305
/**
306
* Test ServerSocket.accept. The ServerSocket uses a platform SocketImpl,
307
* the Socket to accept is created with no SocketImpl, and there is a custom
308
* client SocketImplFactory set.
309
*/
310
public void testServerSocketAccept2() throws IOException {
311
var socket = new Socket((SocketImpl) null) { };
312
assertTrue(getSocketImpl(socket) == null);
313
314
serverSocketAccept(socket, () -> new CustomSocketImpl(false), (ss, s) -> {
315
assertTrue(isPlatformSocketImpl(getSocketImpl(ss)));
316
assertTrue(s == socket);
317
SocketImpl si = getSocketImpl(s);
318
assertTrue(isPlatformSocketImpl(si));
319
checkFields(si);
320
});
321
}
322
323
/**
324
* Test ServerSocket.accept. The ServerSocket uses a platform SocketImpl,
325
* the Socket to accept is created with a SocketImpl that delegates to a
326
* platform SocketImpl.
327
*/
328
public void testServerSocketAccept3() throws IOException {
329
var socket = new Socket();
330
SocketImpl si = getSocketImpl(socket);
331
assertTrue(isSocksSocketImpl(si));
332
SocketImpl delegate = getDelegate(si);
333
assertTrue(isPlatformSocketImpl(delegate));
334
335
serverSocketAccept(socket, (ss, s) -> {
336
assertTrue(isPlatformSocketImpl(getSocketImpl(ss)));
337
assertTrue(s == socket);
338
SocketImpl psi = getSocketImpl(socket);
339
assertTrue(isPlatformSocketImpl(psi));
340
checkFields(psi);
341
});
342
}
343
344
/**
345
* Test ServerSocket.accept. The ServerSocket uses a platform SocketImpl,
346
* the Socket to accept is created with a custom SocketImpl.
347
*/
348
public void testServerSocketAccept4a() throws IOException {
349
SocketImpl clientImpl = new CustomSocketImpl(false);
350
Socket socket = new Socket(clientImpl) { };
351
assertTrue(getSocketImpl(socket) == clientImpl);
352
353
try (ServerSocket ss = serverSocketToAccept(socket)) {
354
expectThrows(IOException.class, ss::accept);
355
} finally {
356
socket.close();
357
}
358
}
359
360
public void testServerSocketAccept4b() throws IOException {
361
SocketImpl clientImpl = new CustomSocketImpl(false);
362
Socket socket = new Socket(clientImpl) { };
363
assertTrue(getSocketImpl(socket) == clientImpl);
364
365
setSocketSocketImplFactory(() -> new CustomSocketImpl(false));
366
try (ServerSocket ss = serverSocketToAccept(socket)) {
367
expectThrows(IOException.class, ss::accept);
368
} finally {
369
setSocketSocketImplFactory(null);
370
socket.close();
371
}
372
}
373
374
/**
375
* Test ServerSocket.accept. The ServerSocket uses a custom SocketImpl,
376
* the Socket to accept is created no SocketImpl.
377
*/
378
public void testServerSocketAccept5a() throws IOException {
379
SocketImpl serverImpl = new CustomSocketImpl(true);
380
try (ServerSocket ss = new ServerSocket(serverImpl) { }) {
381
ss.bind(loopbackSocketAddress());
382
expectThrows(IOException.class, ss::accept);
383
}
384
}
385
386
public void testServerSocketAccept5b() throws IOException {
387
var socket = new Socket((SocketImpl) null) { };
388
assertTrue(getSocketImpl(socket) == null);
389
390
SocketImpl serverImpl = new CustomSocketImpl(true);
391
try (ServerSocket ss = serverSocketToAccept(serverImpl, socket)) {
392
expectThrows(IOException.class, ss::accept);
393
} finally {
394
socket.close();
395
}
396
}
397
398
public void testServerSocketAccept5c() throws IOException {
399
setServerSocketImplFactory(() -> new CustomSocketImpl(true));
400
try (ServerSocket ss = new ServerSocket(0)) {
401
expectThrows(IOException.class, ss::accept);
402
} finally {
403
setServerSocketImplFactory(null);
404
}
405
}
406
407
public void testServerSocketAccept5d() throws IOException {
408
var socket = new Socket((SocketImpl) null) { };
409
assertTrue(getSocketImpl(socket) == null);
410
411
setServerSocketImplFactory(() -> new CustomSocketImpl(true));
412
try (ServerSocket ss = serverSocketToAccept(socket)) {
413
expectThrows(IOException.class, ss::accept);
414
} finally {
415
setServerSocketImplFactory(null);
416
socket.close();
417
}
418
}
419
420
/**
421
* Test ServerSocket.accept. The ServerSocket uses a custom SocketImpl,
422
* the Socket to accept is created with no SocketImpl, and there is a custom
423
* client SocketImplFactory set.
424
*/
425
public void testServerSocketAccept6() throws Exception {
426
var socket = new Socket((SocketImpl) null) { };
427
assertTrue(getSocketImpl(socket) == null);
428
429
SocketImpl serverImpl = new CustomSocketImpl(true);
430
SocketImplFactory clientFactory = () -> new CustomSocketImpl(false);
431
serverSocketAccept(serverImpl, socket, clientFactory, (ss, s) -> {
432
assertTrue(getSocketImpl(ss) == serverImpl);
433
SocketImpl si = getSocketImpl(s);
434
assertTrue(si instanceof CustomSocketImpl);
435
checkFields(si);
436
});
437
}
438
439
/**
440
* Test ServerSocket.accept. The ServerSocket uses a custom SocketImpl,
441
* the Socket to accept is created with a SocketImpl that delegates to a
442
* platform SocketImpl.
443
*/
444
public void testServerSocketAccept7a() throws IOException {
445
var socket = new Socket();
446
SocketImpl si = getSocketImpl(socket);
447
assertTrue(isSocksSocketImpl(si));
448
SocketImpl delegate = getDelegate(si);
449
assertTrue(isPlatformSocketImpl(delegate));
450
451
SocketImpl serverImpl = new CustomSocketImpl(true);
452
try (ServerSocket ss = serverSocketToAccept(serverImpl, socket)) {
453
expectThrows(IOException.class, ss::accept);
454
} finally {
455
socket.close();
456
}
457
}
458
459
public void testServerSocketAccept7b() throws IOException {
460
var socket = new Socket();
461
SocketImpl si = getSocketImpl(socket);
462
assertTrue(isSocksSocketImpl(si));
463
SocketImpl delegate = getDelegate(si);
464
assertTrue(isPlatformSocketImpl(delegate));
465
466
setServerSocketImplFactory(() -> new CustomSocketImpl(true));
467
try (ServerSocket ss = serverSocketToAccept(socket)) {
468
expectThrows(IOException.class, ss::accept);
469
} finally {
470
setServerSocketImplFactory(null);
471
socket.close();
472
}
473
}
474
475
/**
476
* Test ServerSocket.accept. The ServerSocket uses a custom SocketImpl,
477
* the Socket to accept is created with a custom SocketImpl.
478
*/
479
public void testServerSocketAccept8() throws Exception {
480
SocketImpl clientImpl = new CustomSocketImpl(false);
481
Socket socket = new Socket(clientImpl) { };
482
assertTrue(getSocketImpl(socket) == clientImpl);
483
484
SocketImpl serverImpl = new CustomSocketImpl(true);
485
SocketImplFactory clientFactory = () -> new CustomSocketImpl(false);
486
serverSocketAccept(serverImpl, socket, clientFactory, (ss, s) -> {
487
assertTrue(getSocketImpl(ss) == serverImpl);
488
assertTrue(getSocketImpl(s) == clientImpl);
489
checkFields(clientImpl);
490
});
491
}
492
493
/**
494
* Creates a ServerSocket that returns the given Socket from accept.
495
* The consumer is invoked with the server socket and the accepted socket.
496
*/
497
static void serverSocketAccept(Socket socket,
498
BiConsumer<ServerSocket, Socket> consumer)
499
throws IOException
500
{
501
Socket s1 = null;
502
Socket s2 = null;
503
try (ServerSocket ss = serverSocketToAccept(socket)) {
504
s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());
505
s2 = ss.accept();
506
consumer.accept(ss, s2);
507
} finally {
508
if (s1 != null) s1.close();
509
if (s2 != null) s2.close();
510
}
511
}
512
513
/**
514
* Creates a ServerSocket that returns the given Socket from accept. The
515
* given SocketImplFactory is set during the accept and the consumer is
516
* invoked when the server socket and the accepted socket.
517
*/
518
static void serverSocketAccept(Socket socket,
519
SocketImplFactory factory,
520
BiConsumer<ServerSocket, Socket> consumer)
521
throws IOException
522
{
523
Socket s1 = null;
524
Socket s2 = null;
525
try (ServerSocket ss = serverSocketToAccept(socket)) {
526
s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());
527
setSocketSocketImplFactory(factory);
528
try {
529
s2 = ss.accept();
530
} finally {
531
setSocketSocketImplFactory(null);
532
}
533
consumer.accept(ss, s2);
534
} finally {
535
if (s1 != null) s1.close();
536
if (s2 != null) s2.close();
537
}
538
}
539
540
/**
541
* Creates a ServerSocket with a SocketImpl returns the given Socket from
542
* accept. The given SocketImplFactory is set during the accept and the
543
* consumer is invoked when the server socket and the accepted socket.
544
*/
545
static void serverSocketAccept(SocketImpl impl,
546
Socket socket,
547
SocketImplFactory factory,
548
BiConsumer<ServerSocket, Socket> consumer)
549
throws IOException
550
{
551
Socket s1 = null;
552
Socket s2 = null;
553
try (ServerSocket ss = serverSocketToAccept(impl, socket)) {
554
s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());
555
setSocketSocketImplFactory(factory);
556
try {
557
s2 = ss.accept();
558
} finally {
559
setSocketSocketImplFactory(null);
560
}
561
consumer.accept(ss, s2);
562
} finally {
563
if (s1 != null) s1.close();
564
if (s2 != null) s2.close();
565
}
566
}
567
568
/**
569
* Returns a new InetSocketAddress with the loopback interface
570
* and port 0.
571
*/
572
static InetSocketAddress loopbackSocketAddress() {
573
InetAddress loopback = InetAddress.getLoopbackAddress();
574
return new InetSocketAddress(loopback, 0);
575
}
576
577
/**
578
* Returns a ServerSocket bound to a port on the loopback address
579
*/
580
static ServerSocket boundServerSocket() throws IOException {
581
ServerSocket ss = new ServerSocket();
582
ss.bind(loopbackSocketAddress());
583
return ss;
584
}
585
586
/**
587
* Creates a ServerSocket that returns the given Socket from accept.
588
*/
589
static ServerSocket serverSocketToAccept(Socket s) throws IOException {
590
ServerSocket ss = new ServerSocket() {
591
@Override
592
public Socket accept() throws IOException {
593
implAccept(s);
594
return s;
595
}
596
};
597
ss.bind(loopbackSocketAddress());
598
return ss;
599
}
600
601
/**
602
* Creates a ServerSocket with a SocketImpl that returns the given Socket
603
* from accept.
604
*/
605
static ServerSocket serverSocketToAccept(SocketImpl impl, Socket s) throws IOException {
606
ServerSocket ss = new ServerSocket(impl) {
607
@Override
608
public Socket accept() throws IOException {
609
implAccept(s);
610
return s;
611
}
612
};
613
ss.bind(loopbackSocketAddress());
614
return ss;
615
}
616
617
/**
618
* Returns the socket's SocketImpl
619
*/
620
static SocketImpl getSocketImpl(Socket s) {
621
try {
622
Field f = Socket.class.getDeclaredField("impl");
623
f.setAccessible(true);
624
return (SocketImpl) f.get(s);
625
} catch (Exception e) {
626
throw new RuntimeException(e);
627
}
628
}
629
630
/**
631
* Returns the server socket's SocketImpl
632
*/
633
static SocketImpl getSocketImpl(ServerSocket ss) {
634
try {
635
Field f = ServerSocket.class.getDeclaredField("impl");
636
f.setAccessible(true);
637
return (SocketImpl) f.get(ss);
638
} catch (Exception e) {
639
throw new RuntimeException(e);
640
}
641
}
642
643
/**
644
* Returns the SocketImpl that the given SocketImpl delegates to
645
*/
646
static SocketImpl getDelegate(SocketImpl si) {
647
try {
648
Class<?> clazz = Class.forName("java.net.DelegatingSocketImpl");
649
Field f = clazz.getDeclaredField("delegate");
650
f.setAccessible(true);
651
return (SocketImpl) f.get(si);
652
} catch (Exception e) {
653
throw new RuntimeException(e);
654
}
655
}
656
657
/**
658
* Returns the value of a SocketImpl field
659
*/
660
static <T> T get(SocketImpl si, String name) {
661
try {
662
Field f = SocketImpl.class.getDeclaredField(name);
663
f.setAccessible(true);
664
return (T) f.get(si);
665
} catch (Exception e) {
666
throw new RuntimeException(e);
667
}
668
}
669
670
/**
671
* Sets the value of SocketImpl field
672
*/
673
static void set(SocketImpl si, String name, Object value) {
674
try {
675
Field f = SocketImpl.class.getDeclaredField(name);
676
f.setAccessible(true);
677
f.set(si, value);
678
} catch (Exception e) {
679
throw new RuntimeException(e);
680
}
681
}
682
683
/**
684
* Returns true if the SocketImpl is a PlatformSocketImpl
685
*/
686
static boolean isPlatformSocketImpl(SocketImpl si) {
687
try {
688
Class<?> clazz = Class.forName("sun.net.PlatformSocketImpl");
689
return clazz.isInstance(si);
690
} catch (Exception e) {
691
throw new RuntimeException(e);
692
}
693
}
694
695
/**
696
* Returns true if the SocketImpl is a SocksSocketImpl
697
*/
698
static boolean isSocksSocketImpl(SocketImpl si) {
699
try {
700
Class<?> clazz = Class.forName("java.net.SocksSocketImpl");
701
return clazz.isInstance(si);
702
} catch (Exception e) {
703
throw new RuntimeException(e);
704
}
705
}
706
707
/**
708
* Returns true if the SocketImpl is a HttpConnectSocketImpl
709
*/
710
static boolean isHttpConnectSocketImpl(SocketImpl si) {
711
try {
712
Class<?> clazz = Class.forName("java.net.HttpConnectSocketImpl");
713
return clazz.isInstance(si);
714
} catch (Exception e) {
715
throw new RuntimeException(e);
716
}
717
}
718
719
/**
720
* Socket.setSocketImplFactory(SocketImplFactory)
721
*/
722
static void setSocketSocketImplFactory(SocketImplFactory factory) {
723
try {
724
Field f = Socket.class.getDeclaredField("factory");
725
f.setAccessible(true);
726
f.set(null, factory);
727
} catch (Exception e) {
728
throw new RuntimeException(e);
729
}
730
}
731
732
/**
733
* ServerSocket.setSocketFactory(SocketImplFactory)
734
*/
735
static void setServerSocketImplFactory(SocketImplFactory factory) {
736
try {
737
Field f = ServerSocket.class.getDeclaredField("factory");
738
f.setAccessible(true);
739
f.set(null, factory);
740
} catch (Exception e) {
741
throw new RuntimeException(e);
742
}
743
}
744
745
/**
746
* Checks the 4 protected fields of a SocketImpl to make sure that they
747
* have been initialized.
748
*/
749
static void checkFields(SocketImpl si) {
750
FileDescriptor fd = get(si, "fd");
751
InetAddress address = get(si, "address");
752
int port = get(si, "port");
753
int localport = get(si, "localport");
754
assertTrue(fd.valid() && address != null && port != 0 && localport != 0);
755
}
756
757
/**
758
* Custom SocketImpl that is layed on a SocketChannel or ServerSocketChannel
759
*/
760
static class CustomSocketImpl extends SocketImpl {
761
private final boolean server;
762
private ServerSocketChannel ssc;
763
private SocketChannel sc;
764
765
CustomSocketImpl(boolean server) {
766
this.server = server;
767
}
768
769
@Override
770
protected void create(boolean stream) throws IOException {
771
if (server) {
772
ssc = ServerSocketChannel.open();
773
} else {
774
sc = SocketChannel.open();
775
}
776
}
777
778
@Override
779
protected void connect(String host, int port) throws IOException {
780
connect(new InetSocketAddress(host, port), 0);
781
}
782
783
@Override
784
protected void connect(InetAddress address, int port) throws IOException {
785
connect(new InetSocketAddress(address, port), 0);
786
}
787
788
@Override
789
protected void connect(SocketAddress remote, int timeout) throws IOException {
790
sc.connect(remote);
791
super.address = ((InetSocketAddress) remote).getAddress();
792
super.port = ((InetSocketAddress) remote).getPort();
793
}
794
795
@Override
796
protected void bind(InetAddress address, int port) throws IOException {
797
if (server) {
798
ssc.bind(new InetSocketAddress(address, port));
799
super.localport = ssc.socket().getLocalPort();
800
} else {
801
sc.bind(new InetSocketAddress(address, port));
802
super.localport = sc.socket().getLocalPort();
803
}
804
super.address = address;
805
}
806
807
@Override
808
protected void listen(int backlog) {
809
// do nothing
810
}
811
812
@Override
813
protected void accept(SocketImpl si) throws IOException {
814
SocketChannel peer = ssc.accept();
815
FileDescriptor fd;
816
try {
817
Class<?> clazz = Class.forName("sun.nio.ch.SocketChannelImpl");
818
Field f = clazz.getDeclaredField("fd");
819
f.setAccessible(true);
820
fd = (FileDescriptor) f.get(peer);
821
} catch (Exception e) {
822
throw new RuntimeException(e);
823
}
824
set(si, "fd", fd);
825
set(si, "address", peer.socket().getInetAddress());
826
set(si, "port", peer.socket().getPort());
827
set(si, "localport", peer.socket().getLocalPort());
828
}
829
830
@Override
831
protected InputStream getInputStream() {
832
throw new RuntimeException();
833
}
834
835
@Override
836
protected OutputStream getOutputStream() {
837
throw new RuntimeException();
838
}
839
840
@Override
841
protected int available() {
842
return 0;
843
}
844
845
@Override
846
protected void close() {
847
}
848
849
@Override
850
protected void sendUrgentData(int data) {
851
throw new RuntimeException();
852
}
853
854
@Override
855
public void setOption(int option, Object value) {
856
throw new RuntimeException();
857
}
858
859
@Override
860
public Object getOption(int option) {
861
throw new RuntimeException();
862
}
863
}
864
}
865
866