use of javax.net.SocketFactory in project mockito by mockito.
the class DeepStubbingTest method withSimplePrimitive.
/**
* Test that deep stubbing work with primitive expected values
*/
@Test
public void withSimplePrimitive() throws Exception {
int a = 32;
SocketFactory sf = mock(SocketFactory.class, RETURNS_DEEP_STUBS);
when(sf.createSocket().getPort()).thenReturn(a);
assertEquals(a, sf.createSocket().getPort());
}
use of javax.net.SocketFactory in project mockito by mockito.
the class DeepStubbingTest method withAnyPatternArguments.
/**
* Test that deep stubbing work with argument patterns
*/
@Test
public void withAnyPatternArguments() throws Exception {
OutputStream out = new ByteArrayOutputStream();
//TODO: should not use javax in case it changes
SocketFactory sf = mock(SocketFactory.class, RETURNS_DEEP_STUBS);
when(sf.createSocket(anyString(), anyInt()).getOutputStream()).thenReturn(out);
assertSame(out, sf.createSocket("google.com", 80).getOutputStream());
assertSame(out, sf.createSocket("stackoverflow.com", 8080).getOutputStream());
}
use of javax.net.SocketFactory in project keywhiz by square.
the class LdapConnectionFactory method getLDAPConnection.
public LDAPConnection getLDAPConnection(String userDN, String password) throws LDAPException, GeneralSecurityException {
TrustStoreTrustManager trust = new TrustStoreTrustManager(trustStorePath, trustStorePassword.toCharArray(), trustStoreType, false);
LDAPConnectionOptions options = new LDAPConnectionOptions();
options.setSSLSocketVerifier(new HostNameSSLSocketVerifier(false));
SSLUtil sslUtil = new SSLUtil(trust);
SocketFactory factory = new EndpointIdentificationSocketFactory(sslUtil.createSSLSocketFactory("TLSv1.2"));
LDAPConnection ldapConnection = new LDAPConnection(factory, options);
// Connect, retrieve the DN of the user (if any)
ldapConnection.connect(server, port);
ldapConnection.bind(userDN, password);
return ldapConnection;
}
use of javax.net.SocketFactory in project jdk8u_jdk by JetBrains.
the class SslRMIClientSocketFactory method createSocket.
/**
* <p>Creates an SSL socket.</p>
*
* <p>If the system property
* <code>javax.rmi.ssl.client.enabledCipherSuites</code> is
* specified, this method will call {@link
* SSLSocket#setEnabledCipherSuites(String[])} before returning
* the socket. The value of this system property is a string that
* is a comma-separated list of SSL/TLS cipher suites to
* enable.</p>
*
* <p>If the system property
* <code>javax.rmi.ssl.client.enabledProtocols</code> is
* specified, this method will call {@link
* SSLSocket#setEnabledProtocols(String[])} before returning the
* socket. The value of this system property is a string that is a
* comma-separated list of SSL/TLS protocol versions to
* enable.</p>
*/
public Socket createSocket(String host, int port) throws IOException {
// Retrieve the SSLSocketFactory
//
final SocketFactory sslSocketFactory = getDefaultClientSocketFactory();
// Create the SSLSocket
//
final SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(host, port);
// Set the SSLSocket Enabled Cipher Suites
//
final String enabledCipherSuites = System.getProperty("javax.rmi.ssl.client.enabledCipherSuites");
if (enabledCipherSuites != null) {
StringTokenizer st = new StringTokenizer(enabledCipherSuites, ",");
int tokens = st.countTokens();
String[] enabledCipherSuitesList = new String[tokens];
for (int i = 0; i < tokens; i++) {
enabledCipherSuitesList[i] = st.nextToken();
}
try {
sslSocket.setEnabledCipherSuites(enabledCipherSuitesList);
} catch (IllegalArgumentException e) {
throw (IOException) new IOException(e.getMessage()).initCause(e);
}
}
// Set the SSLSocket Enabled Protocols
//
final String enabledProtocols = System.getProperty("javax.rmi.ssl.client.enabledProtocols");
if (enabledProtocols != null) {
StringTokenizer st = new StringTokenizer(enabledProtocols, ",");
int tokens = st.countTokens();
String[] enabledProtocolsList = new String[tokens];
for (int i = 0; i < tokens; i++) {
enabledProtocolsList[i] = st.nextToken();
}
try {
sslSocket.setEnabledProtocols(enabledProtocolsList);
} catch (IllegalArgumentException e) {
throw (IOException) new IOException(e.getMessage()).initCause(e);
}
}
//
return sslSocket;
}
use of javax.net.SocketFactory in project oxCore by GluuFederation.
the class EasySSLProtocolSocketFactory method createSocket.
/**
* Attempts to get a new socket connection to the given host within the
* given time limit.
* <p>
* To circumvent the limitations of older JREs that do not support connect
* timeout a controller thread is executed. The controller thread attempts
* to create a new socket within the given limit of time. If socket
* constructor does not return until the timeout expires, the controller
* terminates and throws an {@link ConnectTimeoutException}
* </p>
*
* @param host
* the host name/IP
* @param port
* the port on the host
* @param localAddress
* the local host name/IP to bind the socket to
* @param localPort
* the port on the local machine
* @param params
* {@link HttpConnectionParams Http connection parameters}
*
* @return Socket a new socket
*
* @throws IOException
* if an I/O error occurs while creating the socket
* @throws UnknownHostException
* if the IP address of the host cannot be determined
*/
public Socket createSocket(final String host, final int port, final InetAddress localAddress, final int localPort, final HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException {
if (params == null) {
throw new IllegalArgumentException("Parameters may not be null");
}
int timeout = params.getConnectionTimeout();
SocketFactory socketfactory = getSSLContext().getSocketFactory();
if (timeout == 0) {
return socketfactory.createSocket(host, port, localAddress, localPort);
} else {
Socket socket = socketfactory.createSocket();
SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
SocketAddress remoteaddr = new InetSocketAddress(host, port);
socket.bind(localaddr);
socket.connect(remoteaddr, timeout);
return socket;
}
}
Aggregations