PageRenderTime 82ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/core/dylib/src/test/java/ch/cyberduck/core/ProxySocketFactoryTest.java

https://gitlab.com/vincent.hsu/cyberduck
Java | 213 lines | 172 code | 17 blank | 24 comment | 4 complexity | e65289a12838172ea67e853546e3d243 MD5 | raw file
  1. package ch.cyberduck.core;
  2. /*
  3. * Copyright (c) 2002-2014 David Kocher. All rights reserved.
  4. * http://cyberduck.io/
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * Bug fixes, suggestions and comments should be sent to:
  17. * feedback@cyberduck.io
  18. */
  19. import ch.cyberduck.core.proxy.Proxy;
  20. import ch.cyberduck.core.proxy.ProxyFinder;
  21. import ch.cyberduck.core.proxy.ProxySocketFactory;
  22. import ch.cyberduck.core.socket.DefaultSocketConfigurator;
  23. import ch.cyberduck.core.socket.SocketConfigurator;
  24. import ch.cyberduck.core.ssl.TrustManagerHostnameCallback;
  25. import ch.cyberduck.test.IntegrationTest;
  26. import org.junit.Ignore;
  27. import org.junit.Test;
  28. import org.junit.experimental.categories.Category;
  29. import java.io.IOException;
  30. import java.net.ConnectException;
  31. import java.net.Inet4Address;
  32. import java.net.Inet6Address;
  33. import java.net.InetAddress;
  34. import java.net.InetSocketAddress;
  35. import java.net.NetworkInterface;
  36. import java.net.Socket;
  37. import java.net.SocketException;
  38. import java.util.Arrays;
  39. import static org.junit.Assert.*;
  40. @Category(IntegrationTest.class)
  41. public class ProxySocketFactoryTest {
  42. @Test
  43. public void testCreateSocketNoProxy() throws Exception {
  44. assertNotNull(new ProxySocketFactory(new TestProtocol(), new TrustManagerHostnameCallback() {
  45. @Override
  46. public String getTarget() {
  47. return "localhost";
  48. }
  49. }).createSocket("localhost", 22));
  50. }
  51. @Test(expected = SocketException.class)
  52. public void testCreateSocketWithProxy() throws Exception {
  53. final Socket socket = new ProxySocketFactory(new TestProtocol(), new TrustManagerHostnameCallback() {
  54. @Override
  55. public String getTarget() {
  56. return "test.cyberduck.ch";
  57. }
  58. }, new DefaultSocketConfigurator(),
  59. new ProxyFinder() {
  60. @Override
  61. public Proxy find(final Host target) {
  62. return new Proxy(Proxy.Type.SOCKS, "localhost", 7000);
  63. }
  64. }).createSocket();
  65. assertNotNull(socket);
  66. socket.connect(new InetSocketAddress("test.cyberduck.ch", 21));
  67. }
  68. @Test
  69. public void testCreateSocketIPv6Localhost() throws Exception {
  70. final Socket socket = new ProxySocketFactory(new TestProtocol(), new TrustManagerHostnameCallback() {
  71. @Override
  72. public String getTarget() {
  73. return "localhost";
  74. }
  75. }).withBlacklistedNetworkInterfaces(Arrays.asList("awdl0")).createSocket("::1", 22);
  76. assertNotNull(socket);
  77. assertTrue(socket.getInetAddress() instanceof Inet6Address);
  78. }
  79. // IPv6 in the test environment
  80. @Test
  81. @Ignore
  82. public void testConnectIPv6LocalAddress() throws Exception {
  83. for(String address : Arrays.asList("fe80::c62c:3ff:fe0b:8670")) {
  84. final Socket socket = new ProxySocketFactory(new TestProtocol(), new TrustManagerHostnameCallback() {
  85. @Override
  86. public String getTarget() {
  87. return "localhost";
  88. }
  89. }).withBlacklistedNetworkInterfaces(Arrays.asList("awdl0")).createSocket(address, 22);
  90. assertNotNull(socket);
  91. assertTrue(socket.getInetAddress() instanceof Inet6Address);
  92. }
  93. }
  94. @Test(expected = SocketException.class)
  95. public void testCreateSocketIPv6LocalAddressConnectionRefused() throws Exception {
  96. for(String address : Arrays.asList("fe80::9272:40ff:fe02:c363")) {
  97. final Socket socket = new ProxySocketFactory(new TestProtocol(), new TrustManagerHostnameCallback() {
  98. @Override
  99. public String getTarget() {
  100. return "localhost";
  101. }
  102. }).withBlacklistedNetworkInterfaces(Arrays.asList("awdl0")).createSocket(address, 22);
  103. }
  104. }
  105. @Test
  106. public void testCreateSocketDualStackGoogle() throws Exception {
  107. final Socket socket = new ProxySocketFactory(new TestProtocol(), new TrustManagerHostnameCallback() {
  108. @Override
  109. public String getTarget() {
  110. return "localhost";
  111. }
  112. }).createSocket("ipv6test.google.com", 80);
  113. assertNotNull(socket);
  114. // We have set `java.net.preferIPv6Addresses` to `false` by default
  115. assertTrue(socket.getInetAddress() instanceof Inet4Address);
  116. }
  117. // IPv6 in the test environment
  118. @Test
  119. @Ignore
  120. public void testCreateSocketIPv6OnlyWithInetAddress() throws Exception {
  121. for(String address : Arrays.asList("ftp6.netbsd.org")) {
  122. final Socket socket = new ProxySocketFactory(new TestProtocol(), new TrustManagerHostnameCallback() {
  123. @Override
  124. public String getTarget() {
  125. return "ftp6.netbsd.org";
  126. }
  127. }, new SocketConfigurator() {
  128. @Override
  129. public void configure(final Socket socket) throws IOException {
  130. assertTrue(socket.getInetAddress() instanceof Inet6Address);
  131. assertEquals(((Inet6Address) socket.getInetAddress()).getScopeId(),
  132. ((Inet6Address) InetAddress.getByName("::1%en0")).getScopedInterface().getIndex());
  133. }
  134. }).withBlacklistedNetworkInterfaces(Arrays.asList("awdl0")).createSocket(address, 21);
  135. assertNotNull(socket);
  136. assertTrue(socket.getInetAddress() instanceof Inet6Address);
  137. }
  138. }
  139. // IPv6 in the test environment
  140. @Test
  141. @Ignore
  142. public void testCreateSocketIPv6OnlyUnknownDestination() throws Exception {
  143. for(String address : Arrays.asList("ftp6.netbsd.org")) {
  144. final Socket socket = new ProxySocketFactory(new TestProtocol(), new TrustManagerHostnameCallback() {
  145. @Override
  146. public String getTarget() {
  147. return "ftp6.netbsd.org";
  148. }
  149. }, new SocketConfigurator() {
  150. @Override
  151. public void configure(final Socket socket) throws IOException {
  152. // Not yet connected
  153. assertNull(socket.getInetAddress());
  154. }
  155. }).withBlacklistedNetworkInterfaces(Arrays.asList("awdl0")).createSocket();
  156. assertNotNull(socket);
  157. assertNull(socket.getInetAddress());
  158. socket.connect(new InetSocketAddress(address, 21), 0);
  159. assertTrue(socket.getInetAddress() instanceof Inet6Address);
  160. assertEquals(((Inet6Address) socket.getInetAddress()).getScopeId(),
  161. ((Inet6Address) InetAddress.getByName("::1%en0")).getScopedInterface().getIndex());
  162. assertTrue(socket.getInetAddress() instanceof Inet6Address);
  163. }
  164. }
  165. @Test
  166. public void testSpecificNetworkInterfaceForIP6Address() throws Exception {
  167. final InetAddress loopback = InetAddress.getByName("::1%en0");
  168. assertNotNull(loopback);
  169. assertTrue(loopback.isLoopbackAddress());
  170. assertTrue(loopback instanceof Inet6Address);
  171. assertEquals(NetworkInterface.getByName("en0").getIndex(),
  172. ((Inet6Address) loopback).getScopedInterface().getIndex());
  173. }
  174. // IPv6 in the test environment
  175. @Test
  176. @Ignore
  177. public void testDefaultNetworkInterfaceForIP6Address() throws Exception {
  178. assertEquals(InetAddress.getByName("::1"), InetAddress.getByName("::1%en0"));
  179. // Bug. Defaults to awdl0 on OS X
  180. assertEquals(((Inet6Address) InetAddress.getByName("::1")).getScopeId(),
  181. ((Inet6Address) InetAddress.getByName("::1%en0")).getScopeId());
  182. }
  183. @Test(expected = ConnectException.class)
  184. public void testFixDefaultNetworkInterface() throws Exception {
  185. final ProxySocketFactory factory = new ProxySocketFactory(new TestProtocol(), new TrustManagerHostnameCallback() {
  186. @Override
  187. public String getTarget() {
  188. return "localhost";
  189. }
  190. }).withBlacklistedNetworkInterfaces(Arrays.asList("awdl0"));
  191. assertEquals(
  192. ((Inet6Address) factory.createSocket("::1%en0", 80).getInetAddress()).getScopeId(),
  193. ((Inet6Address) factory.createSocket("::1", 80).getInetAddress()).getScopeId()
  194. );
  195. }
  196. }