Skip to content

Commit

Permalink
IosSslSocket: implement wrapper socket.
Browse files Browse the repository at this point in the history
	Change on 2018/10/10 by antoniocortes <antoniocortes@google.com>

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=216540484
  • Loading branch information
antonio-cortes-perez committed Nov 19, 2018
1 parent eb8f909 commit bb21406
Show file tree
Hide file tree
Showing 6 changed files with 279 additions and 5 deletions.
10 changes: 10 additions & 0 deletions jre_emul/Classes/com/google/j2objc/net/ssl/IosSslSocket.h
Expand Up @@ -17,6 +17,7 @@
#import "javax/net/ssl/SSLSocket.h"

@class JavaNetInetAddress;
@class JavaNetSocket;

// A socket that uses Apple's SecureTransport API.
@interface ComGoogleJ2objcNetSslIosSslSocket : JavaxNetSslSSLSocket
Expand Down Expand Up @@ -87,6 +88,15 @@ NS_RETURNS_RETAINED;
FOUNDATION_EXPORT ComGoogleJ2objcNetSslIosSslSocket *create_ComGoogleJ2objcNetSslIosSslSocket_initWithJavaNetInetAddress_withInt_withJavaNetInetAddress_withInt_(
JavaNetInetAddress *address, jint port, JavaNetInetAddress *localAddr, jint localPort);

// public IosSslSocket(Socket s, String host, int port, boolean autoClose)
FOUNDATION_EXPORT ComGoogleJ2objcNetSslIosSslSocket *
new_ComGoogleJ2objcNetSslIosSslSocket_initWithJavaNetSocket_withNSString_withInt_withBoolean_(
JavaNetSocket *socket, NSString *host, jint port, jboolean autoClose) NS_RETURNS_RETAINED;

FOUNDATION_EXPORT ComGoogleJ2objcNetSslIosSslSocket *
create_ComGoogleJ2objcNetSslIosSslSocket_initWithJavaNetSocket_withNSString_withInt_withBoolean_(
JavaNetSocket *socket, NSString *host, jint port, jboolean autoClose);

J2OBJC_TYPE_LITERAL_HEADER(ComGoogleJ2objcNetSslIosSslSocket)

#endif
263 changes: 261 additions & 2 deletions jre_emul/Classes/com/google/j2objc/net/ssl/IosSslSocket.m
Expand Up @@ -20,8 +20,10 @@
#import "java/io/OutputStream.h"
#import "java/lang/Exception.h"
#import "java/lang/IllegalArgumentException.h"
#import "java/lang/NullPointerException.h"
#import "java/lang/UnsupportedOperationException.h"
#import "java/net/InetAddress.h"
#import "java/net/Socket.h"
#import "java/net/SocketException.h"
#import "java/io/IOException.h"
#import "jni_util.h"
Expand Down Expand Up @@ -183,6 +185,9 @@ - (void)flush {
@implementation ComGoogleJ2objcNetSslIosSslSocket

+ (void)initialize {
if (self != [ComGoogleJ2objcNetSslIosSslSocket class]) {
return;
}
NSMutableDictionary *temp = [[[NSMutableDictionary alloc] init] autorelease];
NSString *key;
key = [ComGoogleJ2objcSecurityIosSecurityProvider_SslProtocol_get_DEFAULT() description];
Expand Down Expand Up @@ -218,9 +223,10 @@ - (void)dealloc {

- (void)close {
@synchronized(self) {
if ([self isClosed]) return;
tearDownContext(self);
[super close];
if (![self isClosed]) {
[super close];
}
}
}

Expand Down Expand Up @@ -502,4 +508,257 @@ void ComGoogleJ2objcNetSslIosSslSocket_initWithJavaNetInetAddress_withInt_withJa
port, localAddr, localPort)
}

// Delegates most of the calls to the underlying socket.
@interface WrapperSocket : ComGoogleJ2objcNetSslIosSslSocket {
@public
JavaNetSocket *underlyingSocket;
BOOL autoClose;
}
@end

@implementation WrapperSocket

- (void)dealloc {
[super dealloc];
[underlyingSocket release];
}

#pragma mark ComGoogleJ2objcNetSslIosSslSocket methods

- (void)close {
@synchronized(self) {
tearDownContext(self);
if (autoClose && ![underlyingSocket isClosed]) {
[underlyingSocket close];
}
}
}

- (JavaIoInputStream *)plainInputStream {
return [underlyingSocket getInputStream];
}

- (JavaIoOutputStream *)plainOutputStream {
return [underlyingSocket getOutputStream];
}

#pragma mark JavaNetSocket methods

- (void)bindWithJavaNetSocketAddress:(JavaNetSocketAddress *)bindpoint {
[underlyingSocket bindWithJavaNetSocketAddress:bindpoint];
}

- (void)connectWithJavaNetSocketAddress:(JavaNetSocketAddress *)endpoint {
[underlyingSocket connectWithJavaNetSocketAddress:endpoint];
}

- (void)connectWithJavaNetSocketAddress:(JavaNetSocketAddress *)endpoint
withInt:(jint)timeout {
[underlyingSocket connectWithJavaNetSocketAddress:endpoint withInt:timeout];
}

- (JavaNioChannelsSocketChannel *)getChannel {
return [underlyingSocket getChannel];
}

- (JavaIoFileDescriptor *)getFileDescriptor$ {
return [underlyingSocket getFileDescriptor$];
}

- (JavaNetInetAddress *)getInetAddress {
return [underlyingSocket getInetAddress];
}

- (jboolean)getKeepAlive {
return [underlyingSocket getKeepAlive];
}

- (JavaNetInetAddress *)getLocalAddress {
return [underlyingSocket getLocalAddress];
}

- (jint)getLocalPort {
return [underlyingSocket getLocalPort];
}

- (JavaNetSocketAddress *)getLocalSocketAddress {
return [underlyingSocket getLocalSocketAddress];
}

- (jboolean)getOOBInline {
return [underlyingSocket getOOBInline];
}

- (jint)getPort {
return [underlyingSocket getPort];
}

- (jint)getReceiveBufferSize {
return [underlyingSocket getReceiveBufferSize];
}

- (JavaNetSocketAddress *)getRemoteSocketAddress {
return [underlyingSocket getRemoteSocketAddress];
}

- (jboolean)getReuseAddress {
return [underlyingSocket getReuseAddress];
}

- (jint)getSendBufferSize {
return [underlyingSocket getSendBufferSize];
}

- (jint)getSoLinger {
return [underlyingSocket getSoLinger];
}

- (jint)getSoTimeout {
return [underlyingSocket getSoTimeout];
}

- (jboolean)getTcpNoDelay {
return [underlyingSocket getTcpNoDelay];
}

- (jint)getTrafficClass {
return [underlyingSocket getTrafficClass];
}

- (jboolean)isBound {
return [underlyingSocket isBound];
}

- (jboolean)isClosed {
return [underlyingSocket isClosed];
}

- (jboolean)isConnected {
return [underlyingSocket isConnected];
}

- (jboolean)isInputShutdown {
return [underlyingSocket isInputShutdown];
}

- (jboolean)isOutputShutdown {
return [underlyingSocket isOutputShutdown];
}

- (void)sendUrgentDataWithInt:(jint)data {
[underlyingSocket sendUrgentDataWithInt:data];
}

- (void)setKeepAliveWithBoolean:(jboolean)on {
[underlyingSocket setKeepAliveWithBoolean:on];
}

- (void)setOOBInlineWithBoolean:(jboolean)on {
[underlyingSocket setOOBInlineWithBoolean:on];
}

- (void)setPerformancePreferencesWithInt:(jint)connectionTime
withInt:(jint)latency
withInt:(jint)bandwidth {
[underlyingSocket setPerformancePreferencesWithInt:connectionTime
withInt:latency
withInt:bandwidth];
}

- (void)setReceiveBufferSizeWithInt:(jint)size {
[underlyingSocket setReceiveBufferSizeWithInt:size];
}

- (void)setReuseAddressWithBoolean:(jboolean)on {
[underlyingSocket setReuseAddressWithBoolean:on];
}

- (void)setSendBufferSizeWithInt:(jint)size {
[underlyingSocket setSendBufferSizeWithInt:size];
}

- (void)setSoLingerWithBoolean:(jboolean)on
withInt:(jint)linger {
[underlyingSocket setSoLingerWithBoolean:on withInt:linger];
}

- (void)setSoTimeoutWithInt:(jint)timeout {
[underlyingSocket setSoTimeoutWithInt:timeout];
}

- (void)setTcpNoDelayWithBoolean:(jboolean)on {
[underlyingSocket setTcpNoDelayWithBoolean:on];
}

- (void)setTrafficClassWithInt:(jint)tc {
[underlyingSocket setTrafficClassWithInt:tc];
}

- (void)shutdownInput {
[underlyingSocket shutdownInput];
}

- (void)shutdownOutput {
[underlyingSocket shutdownOutput];
}

- (NSString *)description {
return [underlyingSocket description];
}

- (void)createImplWithBoolean:(jboolean)stream {
[underlyingSocket createImplWithBoolean:stream];
}

- (JavaNetSocketImpl *)getImpl {
return [underlyingSocket getImpl];
}

- (void)postAccept {
[underlyingSocket postAccept];
}

- (void)setBound {
[underlyingSocket setBound];
}

- (void)setConnected {
[underlyingSocket setConnected];
}

- (void)setCreated {
[underlyingSocket setCreated];
}

- (void)setImpl {
[underlyingSocket setImpl];
}

@end

// public IosSslSocket(Socket s, String host, int port, boolean autoClose)
void WrapperSocket_initWithJavaNetSocket_initWithNSString_withInt_withBoolean_(
WrapperSocket *self, JavaNetSocket *socket, NSString *host, jint port, jboolean autoClose) {
if (![nil_chk(socket) isConnected]) {
J2ObjCThrowByName(JavaNetSocketException, @"socket is not connected.");
}
init(self);
JreStrongAssign(&self->underlyingSocket, socket);
self->autoClose = autoClose;
}

ComGoogleJ2objcNetSslIosSslSocket *
new_ComGoogleJ2objcNetSslIosSslSocket_initWithJavaNetSocket_withNSString_withInt_withBoolean_(
JavaNetSocket *socket, NSString *host, jint port, jboolean autoClose) {
J2OBJC_NEW_IMPL(WrapperSocket, initWithJavaNetSocket_initWithNSString_withInt_withBoolean_,
socket, host, port, autoClose)
}

ComGoogleJ2objcNetSslIosSslSocket *
create_ComGoogleJ2objcNetSslIosSslSocket_initWithJavaNetSocket_withNSString_withInt_withBoolean_(
JavaNetSocket *socket, NSString *host, jint port, jboolean autoClose) {
J2OBJC_CREATE_IMPL(WrapperSocket, initWithJavaNetSocket_initWithNSString_withInt_withBoolean_,
socket, host, port, autoClose)
}

J2OBJC_CLASS_TYPE_LITERAL_SOURCE(ComGoogleJ2objcNetSslIosSslSocket)
Expand Up @@ -50,7 +50,9 @@ public Socket createSocket() throws IOException {
@Override
public Socket createSocket(Socket s, String host, int port, boolean autoClose)
throws IOException {
throw new UnsupportedOperationException();
IosSslSocket socket = new IosSslSocket(s, host, port, autoClose);
socket.setEnabledProtocols(enabledProtocols);
return socket;
}

@Override
Expand Down
Expand Up @@ -61,7 +61,7 @@ public void test_getDefault() {
/**
* javax.net.ssl.SSLSocketFactory#createSocket(Socket s, String host, int port, boolean autoClose)
*/
public void j2objcNotImplemented_test_createSocket() throws Exception {
public void test_createSocket() throws Exception {
SSLSocketFactory sf = (SSLSocketFactory)SSLSocketFactory.getDefault();
int sport = startServer("test_createSocket()");
int[] invalid = {
Expand Down
Expand Up @@ -221,7 +221,7 @@ public void test_SSLSocketFactory_getSupportedCipherSuitesReturnsCopies() {
assertNotSame(sf.getSupportedCipherSuites(), sf.getSupportedCipherSuites());
}

public void j2objcNotImplemented_test_SSLSocketFactory_createSocket() throws Exception {
public void test_SSLSocketFactory_createSocket() throws Exception {
try {
SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault();
Socket s = sf.createSocket(null, null, -1, false);
Expand Down
Expand Up @@ -16,6 +16,7 @@

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import javax.net.ssl.HandshakeCompletedListener;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
Expand All @@ -36,6 +37,8 @@ public IosSslSocket(InetAddress host, int port) {}

public IosSslSocket(InetAddress address, int port, InetAddress localAddress, int localPort) {}

public IosSslSocket(Socket s, String host, int port, boolean autoClose) {}

@Override
public String[] getSupportedCipherSuites() {
return new String[0];
Expand Down

0 comments on commit bb21406

Please sign in to comment.