equal
deleted
inserted
replaced
66 |
66 |
67 /** |
67 /** |
68 * Test creating a connected Socket, it should be created with a platform SocketImpl. |
68 * Test creating a connected Socket, it should be created with a platform SocketImpl. |
69 */ |
69 */ |
70 public void testNewSocket2() throws IOException { |
70 public void testNewSocket2() throws IOException { |
71 try (ServerSocket ss = new ServerSocket(0)) { |
71 try (ServerSocket ss = boundServerSocket()) { |
72 try (Socket s = new Socket(ss.getInetAddress(), ss.getLocalPort())) { |
72 try (Socket s = new Socket(ss.getInetAddress(), ss.getLocalPort())) { |
73 SocketImpl si = getSocketImpl(s); |
73 SocketImpl si = getSocketImpl(s); |
74 assertTrue(isSocksSocketImpl(si)); |
74 assertTrue(isSocksSocketImpl(si)); |
75 SocketImpl delegate = getDelegate(si); |
75 SocketImpl delegate = getDelegate(si); |
76 assertTrue(isPlatformSocketImpl(delegate)); |
76 assertTrue(isPlatformSocketImpl(delegate)); |
125 */ |
125 */ |
126 public void testNewSocket6() throws IOException { |
126 public void testNewSocket6() throws IOException { |
127 Socket s = new Socket((SocketImpl) null) { }; |
127 Socket s = new Socket((SocketImpl) null) { }; |
128 try (s) { |
128 try (s) { |
129 assertTrue(getSocketImpl(s) == null); |
129 assertTrue(getSocketImpl(s) == null); |
130 s.bind(new InetSocketAddress(0)); // force SocketImpl to be created |
130 s.bind(loopbackSocketAddress()); // force SocketImpl to be created |
131 SocketImpl si = getSocketImpl(s); |
131 SocketImpl si = getSocketImpl(s); |
132 assertTrue(isSocksSocketImpl(si)); |
132 assertTrue(isSocksSocketImpl(si)); |
133 SocketImpl delegate = getDelegate(si); |
133 SocketImpl delegate = getDelegate(si); |
134 assertTrue(isPlatformSocketImpl(delegate)); |
134 assertTrue(isPlatformSocketImpl(delegate)); |
135 } |
135 } |
216 setSocketSocketImplFactory(() -> new CustomSocketImpl(false)); |
216 setSocketSocketImplFactory(() -> new CustomSocketImpl(false)); |
217 try { |
217 try { |
218 Socket s = new Socket((SocketImpl) null) { }; |
218 Socket s = new Socket((SocketImpl) null) { }; |
219 try (s) { |
219 try (s) { |
220 assertTrue(getSocketImpl(s) == null); |
220 assertTrue(getSocketImpl(s) == null); |
221 s.bind(new InetSocketAddress(0)); // force SocketImpl to be created |
221 s.bind(loopbackSocketAddress()); // force SocketImpl to be created |
222 assertTrue(getSocketImpl(s) instanceof CustomSocketImpl); |
222 assertTrue(getSocketImpl(s) instanceof CustomSocketImpl); |
223 } |
223 } |
224 } finally { |
224 } finally { |
225 setSocketSocketImplFactory(null); |
225 setSocketSocketImplFactory(null); |
226 } |
226 } |
376 * the Socket to accept is created no SocketImpl. |
376 * the Socket to accept is created no SocketImpl. |
377 */ |
377 */ |
378 public void testServerSocketAccept5a() throws IOException { |
378 public void testServerSocketAccept5a() throws IOException { |
379 SocketImpl serverImpl = new CustomSocketImpl(true); |
379 SocketImpl serverImpl = new CustomSocketImpl(true); |
380 try (ServerSocket ss = new ServerSocket(serverImpl) { }) { |
380 try (ServerSocket ss = new ServerSocket(serverImpl) { }) { |
381 ss.bind(new InetSocketAddress(0)); |
381 ss.bind(loopbackSocketAddress()); |
382 expectThrows(IOException.class, ss::accept); |
382 expectThrows(IOException.class, ss::accept); |
383 } |
383 } |
384 } |
384 } |
385 |
385 |
386 public void testServerSocketAccept5b() throws IOException { |
386 public void testServerSocketAccept5b() throws IOException { |
564 if (s2 != null) s2.close(); |
564 if (s2 != null) s2.close(); |
565 } |
565 } |
566 } |
566 } |
567 |
567 |
568 /** |
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 /** |
569 * Creates a ServerSocket that returns the given Socket from accept. |
587 * Creates a ServerSocket that returns the given Socket from accept. |
570 */ |
588 */ |
571 static ServerSocket serverSocketToAccept(Socket s) throws IOException { |
589 static ServerSocket serverSocketToAccept(Socket s) throws IOException { |
572 return new ServerSocket(0) { |
590 ServerSocket ss = new ServerSocket() { |
573 @Override |
591 @Override |
574 public Socket accept() throws IOException { |
592 public Socket accept() throws IOException { |
575 implAccept(s); |
593 implAccept(s); |
576 return s; |
594 return s; |
577 } |
595 } |
578 }; |
596 }; |
|
597 ss.bind(loopbackSocketAddress()); |
|
598 return ss; |
579 } |
599 } |
580 |
600 |
581 /** |
601 /** |
582 * Creates a ServerSocket with a SocketImpl that returns the given Socket |
602 * Creates a ServerSocket with a SocketImpl that returns the given Socket |
583 * from accept. |
603 * from accept. |
588 public Socket accept() throws IOException { |
608 public Socket accept() throws IOException { |
589 implAccept(s); |
609 implAccept(s); |
590 return s; |
610 return s; |
591 } |
611 } |
592 }; |
612 }; |
593 ss.bind(new InetSocketAddress(0)); |
613 ss.bind(loopbackSocketAddress()); |
594 return ss; |
614 return ss; |
595 } |
615 } |
596 |
616 |
597 /** |
617 /** |
598 * Returns the socket's SocketImpl |
618 * Returns the socket's SocketImpl |