|
| 1 | +// |
| 2 | +// GCDAsyncSocketConnectionTests.m |
| 3 | +// GCDAsyncSocketConnectionTests |
| 4 | +// |
| 5 | +// Created by Christopher Ballinger on 10/31/14. |
| 6 | +// |
| 7 | +// |
| 8 | + |
| 9 | +#import <Foundation/Foundation.h> |
| 10 | +#import <XCTest/XCTest.h> |
| 11 | +@import CocoaAsyncSocket; |
| 12 | + |
| 13 | +@interface GCDAsyncSocketUNTests : XCTestCase <GCDAsyncSocketDelegate> |
| 14 | +@property (nonatomic, strong) NSURL *url; |
| 15 | +@property (nonatomic, strong) GCDAsyncSocket *clientSocket; |
| 16 | +@property (nonatomic, strong) GCDAsyncSocket *serverSocket; |
| 17 | +@property (nonatomic, strong) GCDAsyncSocket *acceptedServerSocket; |
| 18 | +@property (nonatomic, strong) NSData *readData; |
| 19 | + |
| 20 | +@property (nonatomic, strong) XCTestExpectation *expectation; |
| 21 | +@end |
| 22 | + |
| 23 | +@implementation GCDAsyncSocketUNTests |
| 24 | + |
| 25 | +- (void)setUp { |
| 26 | + [super setUp]; |
| 27 | + // Put setup code here. This method is called before the invocation of each test method in the class. |
| 28 | + self.url = [NSURL fileURLWithPath:@"/tmp/GCDAsyncSocketUNTests"]; |
| 29 | + self.clientSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()]; |
| 30 | + self.serverSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()]; |
| 31 | +} |
| 32 | + |
| 33 | +- (void)tearDown { |
| 34 | + // Put teardown code here. This method is called after the invocation of each test method in the class. |
| 35 | + [super tearDown]; |
| 36 | + [self.clientSocket disconnect]; |
| 37 | + [self.serverSocket disconnect]; |
| 38 | + [self.acceptedServerSocket disconnect]; |
| 39 | + self.clientSocket = nil; |
| 40 | + self.serverSocket = nil; |
| 41 | + self.acceptedServerSocket = nil; |
| 42 | + [[NSFileManager defaultManager] removeItemAtURL:self.url error:nil]; |
| 43 | +} |
| 44 | + |
| 45 | +- (void)testFullConnection { |
| 46 | + NSError *error = nil; |
| 47 | + BOOL success = NO; |
| 48 | + success = [self.serverSocket acceptOnUrl:self.url error:&error]; |
| 49 | + XCTAssertTrue(success, @"Server failed setting up socket at path %@ %@", self.url.path, error); |
| 50 | + success = [self.clientSocket connectToUrl:self.url withTimeout:-1 error:&error]; |
| 51 | + XCTAssertTrue(success, @"Client failed connecting to server socket at path %@ %@", self.url.path, error); |
| 52 | + |
| 53 | + self.expectation = [self expectationWithDescription:@"Test Full Connection"]; |
| 54 | + [self waitForExpectationsWithTimeout:30 handler:^(NSError *error) { |
| 55 | + if (error) { |
| 56 | + NSLog(@"Error establishing test connection"); |
| 57 | + } |
| 58 | + }]; |
| 59 | +} |
| 60 | + |
| 61 | +- (void)testTransferFromClient { |
| 62 | + |
| 63 | + NSData *testData = [@"ThisTestRocks!!!" dataUsingEncoding:NSUTF8StringEncoding]; |
| 64 | + |
| 65 | + // set up and conncet to socket |
| 66 | + [self.serverSocket acceptOnUrl:self.url error:nil]; |
| 67 | + [self.clientSocket connectToUrl:self.url withTimeout:-1 error:nil]; |
| 68 | + |
| 69 | + // wait for connection |
| 70 | + self.expectation = [self expectationWithDescription:@"Socket Connected"]; |
| 71 | + [self waitForExpectationsWithTimeout:30 handler:^(NSError *error) { |
| 72 | + |
| 73 | + // start reading |
| 74 | + [self.acceptedServerSocket readDataWithTimeout:-1 tag:0]; |
| 75 | + |
| 76 | + // send data |
| 77 | + self.expectation = [self expectationWithDescription:@"Data Sent"]; |
| 78 | + [self.clientSocket writeData:testData withTimeout:-1 tag:0]; |
| 79 | + [self waitForExpectationsWithTimeout:1 handler:^(NSError *error) { |
| 80 | + if (error) { |
| 81 | + return NSLog(@"Error reading data"); |
| 82 | + } |
| 83 | + XCTAssertTrue([testData isEqual:self.readData], @"Read data did not match test data"); |
| 84 | + }]; |
| 85 | + }]; |
| 86 | +} |
| 87 | + |
| 88 | +- (void)testTransferFromServer { |
| 89 | + |
| 90 | + NSData *testData = [@"ThisTestRocks!!!" dataUsingEncoding:NSUTF8StringEncoding]; |
| 91 | + |
| 92 | + // set up and conncet to socket |
| 93 | + [self.serverSocket acceptOnUrl:self.url error:nil]; |
| 94 | + [self.clientSocket connectToUrl:self.url withTimeout:-1 error:nil]; |
| 95 | + |
| 96 | + // wait for connection |
| 97 | + self.expectation = [self expectationWithDescription:@"Socket Connected"]; |
| 98 | + [self waitForExpectationsWithTimeout:30 handler:^(NSError *error) { |
| 99 | + |
| 100 | + // start reading |
| 101 | + [self.clientSocket readDataWithTimeout:-1 tag:0]; |
| 102 | + |
| 103 | + // send data |
| 104 | + self.expectation = [self expectationWithDescription:@"Data Sent"]; |
| 105 | + [self.acceptedServerSocket writeData:testData withTimeout:-1 tag:0]; |
| 106 | + [self waitForExpectationsWithTimeout:1 handler:^(NSError *error) { |
| 107 | + if (error) { |
| 108 | + return NSLog(@"Error reading data"); |
| 109 | + } |
| 110 | + XCTAssertTrue([testData isEqual:self.readData], @"Read data did not match test data"); |
| 111 | + }]; |
| 112 | + }]; |
| 113 | +} |
| 114 | + |
| 115 | +#pragma mark GCDAsyncSocketDelegate methods |
| 116 | + |
| 117 | +/** |
| 118 | + * Called when a socket accepts a connection. |
| 119 | + * Another socket is automatically spawned to handle it. |
| 120 | + * |
| 121 | + * You must retain the newSocket if you wish to handle the connection. |
| 122 | + * Otherwise the newSocket instance will be released and the spawned connection will be closed. |
| 123 | + * |
| 124 | + * By default the new socket will have the same delegate and delegateQueue. |
| 125 | + * You may, of course, change this at any time. |
| 126 | + **/ |
| 127 | +- (void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSocket { |
| 128 | + NSLog(@"didAcceptNewSocket %@ %@", sock, newSocket); |
| 129 | + self.acceptedServerSocket = newSocket; |
| 130 | +} |
| 131 | + |
| 132 | +/** |
| 133 | + * Called when a socket connects and is ready for reading and writing. |
| 134 | + * The host parameter will be an IP address, not a DNS name. |
| 135 | + **/ |
| 136 | +- (void)socket:(GCDAsyncSocket *)sock didConnectToUrl:(NSURL *)url { |
| 137 | + NSLog(@"didConnectToUrl %@", url); |
| 138 | + [self.expectation fulfill]; |
| 139 | +} |
| 140 | + |
| 141 | +/** |
| 142 | + * Called when a socket has completed reading the requested data into memory. |
| 143 | + * Not called if there is an error. |
| 144 | + **/ |
| 145 | +- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag { |
| 146 | + NSLog(@"didReadData: %@ tag: %ld", data, tag); |
| 147 | + self.readData = data; |
| 148 | + [self.expectation fulfill]; |
| 149 | +} |
| 150 | + |
| 151 | + |
| 152 | +@end |
0 commit comments