diff -r 000000000000 -r 9d67172bb323 BLIP/BLIPTest.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BLIP/BLIPTest.m Fri May 23 17:37:36 2008 -0700 @@ -0,0 +1,350 @@ +// +// BLIPTest.m +// MYNetwork +// +// Created by Jens Alfke on 5/13/08. +// Copyright 2008 Jens Alfke. All rights reserved. +// + +#ifndef NDEBUG + + +#import "BLIPRequest.h" +#import "BLIPProperties.h" +#import "BLIPConnection.h" +#import "IPAddress.h" +#import "Target.h" + +#define HAVE_KEYCHAIN_FRAMEWORK 0 +#if HAVE_KEYCHAIN_FRAMEWORK +#import +#endif + +#define kListenerPort 46353 +#define kSendInterval 0.5 +#define kNBatchedMessages 20 +#define kUseCompression YES +#define kUrgentEvery 4 +#define kClientRequiresSSL NO +#define kClientUsesSSLCert NO +#define kListenerRequiresSSL NO +#define kListenerRequiresClientCert NO + + +static SecIdentityRef GetClientIdentity(void) { + return NULL; // Make this return a valid identity to test client-side certs +} + +static SecIdentityRef GetListenerIdentity(void) { + return NULL; // Make this return a valid identity to test client-side certs +} + + +#pragma mark - +#pragma mark CLIENT TEST: + + +@interface BLIPConnectionTester : NSObject +{ + BLIPConnection *_conn; + NSMutableDictionary *_pending; +} + +@end + + +@implementation BLIPConnectionTester + +- (id) init +{ + self = [super init]; + if (self != nil) { + Log(@"** INIT %@",self); + _pending = [[NSMutableDictionary alloc] init]; + IPAddress *addr = [[IPAddress alloc] initWithHostname: @"localhost" port: kListenerPort]; + _conn = [[BLIPConnection alloc] initToAddress: addr]; + if( ! _conn ) { + [self release]; + return nil; + } + if( kClientRequiresSSL ) { + _conn.SSLProperties = $mdict({kTCPPropertySSLAllowsAnyRoot, $true}); + if( kClientUsesSSLCert ) { + SecIdentityRef clientIdentity = GetClientIdentity(); + if( clientIdentity ) { + [_conn setSSLProperty: $array((id)clientIdentity) + forKey: kTCPPropertySSLCertificates]; + } + } + } + _conn.delegate = self; + Log(@"** Opening connection..."); + [_conn open]; + } + return self; +} + +- (void) dealloc +{ + Log(@"** %@ closing",self); + [_conn close]; + [_conn release]; + [super dealloc]; +} + +- (void) sendAMessage +{ + Log(@"** Sending another %i messages...", kNBatchedMessages); + for( int i=0; i 12 ) + q.urgent = YES; + BLIPResponse *response = [q send]; + Assert(response); + Assert(q.number>0); + Assert(response.number==q.number); + [_pending setObject: $object(size) forKey: $object(q.number)]; + response.onComplete = $target(self,responseArrived:); + } + [self performSelector: @selector(sendAMessage) withObject: nil afterDelay: kSendInterval]; +} + +- (void) responseArrived: (BLIPResponse*)response +{ + Log(@"********** called responseArrived: %@",response); +} + +- (void) connectionDidOpen: (TCPConnection*)connection +{ + Log(@"** %@ didOpen",connection); + [self sendAMessage]; +} +- (BOOL) connection: (TCPConnection*)connection authorizeSSLPeer: (SecCertificateRef)peerCert +{ +#if HAVE_KEYCHAIN_FRAMEWORK + Certificate *cert = peerCert ?[Certificate certificateWithCertificateRef: peerCert] :nil; + Log(@"** %@ authorizeSSLPeer: %@",self,cert); +#else + Log(@"** %@ authorizeSSLPeer: %@",self,peerCert); +#endif + return peerCert != nil; +} +- (void) connection: (TCPConnection*)connection failedToOpen: (NSError*)error +{ + Log(@"** %@ failedToOpen: %@",connection,error); + CFRunLoopStop(CFRunLoopGetCurrent()); +} +- (void) connectionDidClose: (TCPConnection*)connection +{ + Log(@"** %@ didClose",connection); + setObj(&_conn,nil); + [NSObject cancelPreviousPerformRequestsWithTarget: self]; + CFRunLoopStop(CFRunLoopGetCurrent()); +} +- (void) connection: (BLIPConnection*)connection receivedRequest: (BLIPRequest*)request +{ + Log(@"***** %@ received %@",connection,request); + [request respondWithData: request.body]; +} + +- (void) connection: (BLIPConnection*)connection receivedResponse: (BLIPResponse*)response +{ + Log(@"********** %@ received %@",connection,response); + NSNumber *sizeObj = [_pending objectForKey: $object(response.number)]; + + if( response.error ) + Warn(@"Got error response: %@",response.error); + else { + NSData *body = response.body; + size_t size = body.length; + Assert(size<32768); + const UInt8 *bytes = body.bytes; + for( size_t i=0; i +{ + BLIPListener *_listener; +} + +@end + + +@implementation BLIPTestListener + +- (id) init +{ + self = [super init]; + if (self != nil) { + _listener = [[BLIPListener alloc] initWithPort: kListenerPort]; + _listener.delegate = self; + _listener.pickAvailablePort = YES; + _listener.bonjourServiceType = @"_bliptest._tcp"; + if( kListenerRequiresSSL ) { + SecIdentityRef listenerIdentity = GetListenerIdentity(); + Assert(listenerIdentity); + _listener.SSLProperties = $mdict({kTCPPropertySSLCertificates, $array((id)listenerIdentity)}, + {kTCPPropertySSLAllowsAnyRoot,$true}, + {kTCPPropertySSLClientSideAuthentication, $object(kTryAuthenticate)}); + } + Assert( [_listener open] ); + Log(@"%@ is listening...",self); + } + return self; +} + +- (void) dealloc +{ + Log(@"%@ closing",self); + [_listener close]; + [_listener release]; + [super dealloc]; +} + +- (void) listener: (TCPListener*)listener didAcceptConnection: (TCPConnection*)connection +{ + Log(@"** %@ accepted %@",self,connection); + connection.delegate = self; +} + +- (void) listener: (TCPListener*)listener failedToOpen: (NSError*)error +{ + Log(@"** BLIPTestListener failed to open: %@",error); +} + +- (void) listenerDidOpen: (TCPListener*)listener {Log(@"** BLIPTestListener did open");} +- (void) listenerDidClose: (TCPListener*)listener {Log(@"** BLIPTestListener did close");} + +- (BOOL) listener: (TCPListener*)listener shouldAcceptConnectionFrom: (IPAddress*)address +{ + Log(@"** %@ shouldAcceptConnectionFrom: %@",self,address); + return YES; +} + + +- (void) connectionDidOpen: (TCPConnection*)connection +{ + Log(@"** %@ didOpen [SSL=%@]",connection,connection.actualSecurityLevel); +} +- (BOOL) connection: (TCPConnection*)connection authorizeSSLPeer: (SecCertificateRef)peerCert +{ +#if HAVE_KEYCHAIN_FRAMEWORK + Certificate *cert = peerCert ?[Certificate certificateWithCertificateRef: peerCert] :nil; + Log(@"** %@ authorizeSSLPeer: %@",connection,cert); +#else + Log(@"** %@ authorizeSSLPeer: %@",self,peerCert); +#endif + return peerCert != nil || ! kListenerRequiresClientCert; +} +- (void) connection: (TCPConnection*)connection failedToOpen: (NSError*)error +{ + Log(@"** %@ failedToOpen: %@",connection,error); +} +- (void) connectionDidClose: (TCPConnection*)connection +{ + Log(@"** %@ didClose",connection); + [connection release]; +} +- (void) connection: (BLIPConnection*)connection receivedRequest: (BLIPRequest*)request +{ + Log(@"***** %@ received %@",connection,request); + NSData *body = request.body; + size_t size = body.length; + Assert(size<32768); + const UInt8 *bytes = body.bytes; + for( size_t i=0; i. All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, are permitted + provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this list of conditions + and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, this list of conditions + and the following disclaimer in the documentation and/or other materials provided with the + distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI- + BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */