mirror of
https://github.com/owncloud/android-library.git
synced 2025-06-07 16:06:08 +00:00
Refactored test project to work correctly with AdvancedSslSocketFactory as base to create sockets
This commit is contained in:
parent
5a67c8b4fe
commit
5f11b3d998
@ -78,8 +78,11 @@ public class AdvancedSslSocketFactory implements SecureProtocolSocketFactory {
|
|||||||
|
|
||||||
if (sslContext == null)
|
if (sslContext == null)
|
||||||
throw new IllegalArgumentException("AdvancedSslSocketFactory can not be created with a null SSLContext");
|
throw new IllegalArgumentException("AdvancedSslSocketFactory can not be created with a null SSLContext");
|
||||||
if (trustManager == null)
|
if (trustManager == null && mHostnameVerifier != null)
|
||||||
throw new IllegalArgumentException("AdvancedSslSocketFactory can not be created with a null Trust Manager");
|
throw new IllegalArgumentException(
|
||||||
|
"AdvancedSslSocketFactory can not be created with a null Trust Manager and a " +
|
||||||
|
"not null Hostname Verifier"
|
||||||
|
);
|
||||||
mSslContext = sslContext;
|
mSslContext = sslContext;
|
||||||
mTrustManager = trustManager;
|
mTrustManager = trustManager;
|
||||||
mHostnameVerifier = hostnameVerifier;
|
mHostnameVerifier = hostnameVerifier;
|
||||||
|
@ -26,9 +26,7 @@ package com.owncloud.android.lib.test_project;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.InetSocketAddress;
|
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.net.SocketAddress;
|
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.security.GeneralSecurityException;
|
import java.security.GeneralSecurityException;
|
||||||
import java.security.KeyStore;
|
import java.security.KeyStore;
|
||||||
@ -38,9 +36,7 @@ import java.security.cert.CertStoreException;
|
|||||||
import java.security.cert.CertificateException;
|
import java.security.cert.CertificateException;
|
||||||
import java.security.cert.X509Certificate;
|
import java.security.cert.X509Certificate;
|
||||||
|
|
||||||
import javax.net.SocketFactory;
|
|
||||||
import javax.net.ssl.SSLContext;
|
import javax.net.ssl.SSLContext;
|
||||||
import javax.net.ssl.SSLSocket;
|
|
||||||
import javax.net.ssl.TrustManager;
|
import javax.net.ssl.TrustManager;
|
||||||
import javax.net.ssl.TrustManagerFactory;
|
import javax.net.ssl.TrustManagerFactory;
|
||||||
import javax.net.ssl.X509TrustManager;
|
import javax.net.ssl.X509TrustManager;
|
||||||
@ -49,7 +45,7 @@ import org.apache.commons.httpclient.ConnectTimeoutException;
|
|||||||
import org.apache.commons.httpclient.params.HttpConnectionParams;
|
import org.apache.commons.httpclient.params.HttpConnectionParams;
|
||||||
import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
|
import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
|
||||||
|
|
||||||
import com.owncloud.android.lib.common.network.ServerNameIndicator;
|
import com.owncloud.android.lib.common.network.AdvancedSslSocketFactory;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -64,7 +60,8 @@ import com.owncloud.android.lib.common.network.ServerNameIndicator;
|
|||||||
public class SelfSignedConfidentSslSocketFactory implements SecureProtocolSocketFactory {
|
public class SelfSignedConfidentSslSocketFactory implements SecureProtocolSocketFactory {
|
||||||
|
|
||||||
|
|
||||||
private SSLContext mSslContext = null;
|
//private SSLContext mSslContext = null;
|
||||||
|
private AdvancedSslSocketFactory mWrappedSslSocketFactory = null;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -72,7 +69,13 @@ public class SelfSignedConfidentSslSocketFactory implements SecureProtocolSocket
|
|||||||
* @throws GeneralSecurityException
|
* @throws GeneralSecurityException
|
||||||
*/
|
*/
|
||||||
public SelfSignedConfidentSslSocketFactory() throws GeneralSecurityException {
|
public SelfSignedConfidentSslSocketFactory() throws GeneralSecurityException {
|
||||||
mSslContext = createSslContext();
|
SSLContext sslContext = SSLContext.getInstance("TLS");
|
||||||
|
sslContext.init(
|
||||||
|
null,
|
||||||
|
new TrustManager[] { new SelfSignedConfidentX509TrustManager() },
|
||||||
|
null
|
||||||
|
);
|
||||||
|
mWrappedSslSocketFactory = new AdvancedSslSocketFactory(sslContext, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -81,7 +84,7 @@ public class SelfSignedConfidentSslSocketFactory implements SecureProtocolSocket
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
|
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
|
||||||
return mSslContext.getSocketFactory().createSocket(host, port);
|
return mWrappedSslSocketFactory.createSocket(host, port);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -90,7 +93,7 @@ public class SelfSignedConfidentSslSocketFactory implements SecureProtocolSocket
|
|||||||
@Override
|
@Override
|
||||||
public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort)
|
public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort)
|
||||||
throws IOException, UnknownHostException {
|
throws IOException, UnknownHostException {
|
||||||
return mSslContext.getSocketFactory().createSocket(host, port, clientHost, clientPort);
|
return mWrappedSslSocketFactory.createSocket(host, port, clientHost, clientPort);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -112,19 +115,7 @@ public class SelfSignedConfidentSslSocketFactory implements SecureProtocolSocket
|
|||||||
HttpConnectionParams params) throws IOException, UnknownHostException,
|
HttpConnectionParams params) throws IOException, UnknownHostException,
|
||||||
ConnectTimeoutException {
|
ConnectTimeoutException {
|
||||||
|
|
||||||
if (params == null) {
|
return mWrappedSslSocketFactory.createSocket(host, port, localAddress, localPort, params);
|
||||||
throw new IllegalArgumentException("Parameters may not be null");
|
|
||||||
}
|
|
||||||
int timeout = params.getConnectionTimeout();
|
|
||||||
SocketFactory socketfactory = mSslContext.getSocketFactory();
|
|
||||||
Socket socket = socketfactory.createSocket();
|
|
||||||
SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
|
|
||||||
SocketAddress remoteaddr = new InetSocketAddress(host, port);
|
|
||||||
socket.setSoTimeout(params.getSoTimeout());
|
|
||||||
socket.bind(localaddr);
|
|
||||||
ServerNameIndicator.setServerNameIndication(host, (SSLSocket)socket);
|
|
||||||
socket.connect(remoteaddr, timeout);
|
|
||||||
return socket;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -133,20 +124,10 @@ public class SelfSignedConfidentSslSocketFactory implements SecureProtocolSocket
|
|||||||
@Override
|
@Override
|
||||||
public Socket createSocket(Socket socket, String host, int port, boolean autoClose)
|
public Socket createSocket(Socket socket, String host, int port, boolean autoClose)
|
||||||
throws IOException, UnknownHostException {
|
throws IOException, UnknownHostException {
|
||||||
return mSslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
|
return mWrappedSslSocketFactory.createSocket(socket, host, port, autoClose);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private static SSLContext createSslContext() throws GeneralSecurityException {
|
|
||||||
SSLContext context = SSLContext.getInstance("TLS");
|
|
||||||
context.init(
|
|
||||||
null,
|
|
||||||
new TrustManager[] {new SelfSignedConfidentX509TrustManager()},
|
|
||||||
null);
|
|
||||||
return context;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class SelfSignedConfidentX509TrustManager implements X509TrustManager {
|
public static class SelfSignedConfidentX509TrustManager implements X509TrustManager {
|
||||||
|
|
||||||
private X509TrustManager mStandardTrustManager = null;
|
private X509TrustManager mStandardTrustManager = null;
|
||||||
|
@ -188,7 +188,7 @@ public class TestActivity extends Activity {
|
|||||||
public RemoteOperationResult removeFile(String remotePath) {
|
public RemoteOperationResult removeFile(String remotePath) {
|
||||||
RemoveRemoteFileOperation removeOperation = new RemoveRemoteFileOperation(remotePath);
|
RemoveRemoteFileOperation removeOperation = new RemoveRemoteFileOperation(remotePath);
|
||||||
RemoteOperationResult result = removeOperation.execute(mClient);
|
RemoteOperationResult result = removeOperation.execute(mClient);
|
||||||
return TestActivity.removeFile(remotePath, mClient);
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user