Skip to content

Commit 6942918

Browse files
committed
added tests
1 parent 16c117f commit 6942918

File tree

3 files changed

+226
-0
lines changed

3 files changed

+226
-0
lines changed

Tests/CocoaAsyncSocket.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88

99
/* Begin PBXBuildFile section */
1010
112EB90908C5A2F61A4882CB /* Pods_CocoaAsyncSocketTestsMac.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 22AFE0AC4A8210F5D90D8419 /* Pods_CocoaAsyncSocketTestsMac.framework */; settings = {ATTRIBUTES = (Weak, ); }; };
11+
2DBCA5C81B8CF4F3004F3128 /* GCDAsyncSocketUNTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 2DBCA5C71B8CF4F3004F3128 /* GCDAsyncSocketUNTests.m */; };
1112
673F792AB48B8FDEE8B23C9B /* Pods_CocoaAsyncSocketTestsiOS.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F95D257221C81668EA412B30 /* Pods_CocoaAsyncSocketTestsiOS.framework */; settings = {ATTRIBUTES = (Weak, ); }; };
1213
D938B4E41B752ED500FE8AB3 /* GCDAsyncSocketConnectionTests.m in Sources */ = {isa = PBXBuildFile; fileRef = D938B4E31B752ED500FE8AB3 /* GCDAsyncSocketConnectionTests.m */; };
1314
D938B4E51B752ED500FE8AB3 /* GCDAsyncSocketConnectionTests.m in Sources */ = {isa = PBXBuildFile; fileRef = D938B4E31B752ED500FE8AB3 /* GCDAsyncSocketConnectionTests.m */; };
1415
/* End PBXBuildFile section */
1516

1617
/* Begin PBXFileReference section */
1718
22AFE0AC4A8210F5D90D8419 /* Pods_CocoaAsyncSocketTestsMac.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_CocoaAsyncSocketTestsMac.framework; sourceTree = BUILT_PRODUCTS_DIR; };
19+
2DBCA5C71B8CF4F3004F3128 /* GCDAsyncSocketUNTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = GCDAsyncSocketUNTests.m; path = Mac/GCDAsyncSocketUNTests.m; sourceTree = SOURCE_ROOT; };
1820
352DA76AF29EE988F653F1BD /* Pods-CocoaAsyncSocketTestsiOS.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-CocoaAsyncSocketTestsiOS.release.xcconfig"; path = "Pods/Target Support Files/Pods-CocoaAsyncSocketTestsiOS/Pods-CocoaAsyncSocketTestsiOS.release.xcconfig"; sourceTree = "<group>"; };
1921
BD85483ACFF4A9D05C841806 /* Pods-CocoaAsyncSocketTestsMac.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-CocoaAsyncSocketTestsMac.debug.xcconfig"; path = "Pods/Target Support Files/Pods-CocoaAsyncSocketTestsMac/Pods-CocoaAsyncSocketTestsMac.debug.xcconfig"; sourceTree = "<group>"; };
2022
C9198C88D67BDEFEA629AB60 /* Pods-CocoaAsyncSocketTestsMac.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-CocoaAsyncSocketTestsMac.release.xcconfig"; path = "Pods/Target Support Files/Pods-CocoaAsyncSocketTestsMac/Pods-CocoaAsyncSocketTestsMac.release.xcconfig"; sourceTree = "<group>"; };
@@ -113,6 +115,7 @@
113115
D9BC0D8E1A0458EF0059D906 /* Mac */ = {
114116
isa = PBXGroup;
115117
children = (
118+
2DBCA5C71B8CF4F3004F3128 /* GCDAsyncSocketUNTests.m */,
116119
D9BC0D8F1A0458EF0059D906 /* Supporting Files */,
117120
);
118121
name = Mac;
@@ -338,6 +341,7 @@
338341
isa = PBXSourcesBuildPhase;
339342
buildActionMask = 2147483647;
340343
files = (
344+
2DBCA5C81B8CF4F3004F3128 /* GCDAsyncSocketUNTests.m in Sources */,
341345
D938B4E51B752ED500FE8AB3 /* GCDAsyncSocketConnectionTests.m in Sources */,
342346
);
343347
runOnlyForDeploymentPostprocessing = 0;

Tests/Mac/GCDAsyncSocketUNTests.m

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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

Tests/Shared/GCDAsyncSocketConnectionTests.m

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ @interface GCDAsyncSocketConnectionTests : XCTestCase <GCDAsyncSocketDelegate>
1616
@property (nonatomic, strong) GCDAsyncSocket *clientSocket;
1717
@property (nonatomic, strong) GCDAsyncSocket *serverSocket;
1818
@property (nonatomic, strong) GCDAsyncSocket *acceptedServerSocket;
19+
@property (nonatomic, strong) NSData *readData;
1920

2021
@property (nonatomic, strong) XCTestExpectation *expectation;
2122
@end
@@ -56,6 +57,60 @@ - (void)testFullConnection {
5657
}];
5758
}
5859

60+
- (void)testTransferFromClient {
61+
62+
NSData *testData = [@"ThisTestRocks!!!" dataUsingEncoding:NSUTF8StringEncoding];
63+
64+
// set up and conncet to socket
65+
[self.serverSocket acceptOnPort:kTestPort error:nil];
66+
[self.clientSocket connectToHost:@"127.0.0.1" onPort:kTestPort error:nil];
67+
68+
// wait for connection
69+
self.expectation = [self expectationWithDescription:@"Socket Connected"];
70+
[self waitForExpectationsWithTimeout:30 handler:^(NSError *error) {
71+
72+
// start reading
73+
[self.acceptedServerSocket readDataWithTimeout:-1 tag:0];
74+
75+
// send data
76+
self.expectation = [self expectationWithDescription:@"Data Sent"];
77+
[self.clientSocket writeData:testData withTimeout:-1 tag:0];
78+
[self waitForExpectationsWithTimeout:1 handler:^(NSError *error) {
79+
if (error) {
80+
return NSLog(@"Error reading data");
81+
}
82+
XCTAssertTrue([testData isEqual:self.readData], @"Read data did not match test data");
83+
}];
84+
}];
85+
}
86+
87+
- (void)testTransferFromServer {
88+
89+
NSData *testData = [@"ThisTestRocks!!!" dataUsingEncoding:NSUTF8StringEncoding];
90+
91+
// set up and conncet to socket
92+
[self.serverSocket acceptOnPort:kTestPort error:nil];
93+
[self.clientSocket connectToHost:@"127.0.0.1" onPort:kTestPort error:nil];
94+
95+
// wait for connection
96+
self.expectation = [self expectationWithDescription:@"Socket Connected"];
97+
[self waitForExpectationsWithTimeout:30 handler:^(NSError *error) {
98+
99+
// start reading
100+
[self.clientSocket readDataWithTimeout:-1 tag:0];
101+
102+
// send data
103+
self.expectation = [self expectationWithDescription:@"Data Sent"];
104+
[self.acceptedServerSocket writeData:testData withTimeout:-1 tag:0];
105+
[self waitForExpectationsWithTimeout:1 handler:^(NSError *error) {
106+
if (error) {
107+
return NSLog(@"Error reading data");
108+
}
109+
XCTAssertTrue([testData isEqual:self.readData], @"Read data did not match test data");
110+
}];
111+
}];
112+
}
113+
59114
#pragma mark GCDAsyncSocketDelegate methods
60115

61116
/**
@@ -82,6 +137,21 @@ - (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(ui
82137
[self.expectation fulfill];
83138
}
84139

140+
/**
141+
* Called when a socket has completed reading the requested data into memory.
142+
* Not called if there is an error.
143+
**/
144+
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag {
145+
NSLog(@"didReadData: %@ withTag: %ld", data, tag);
146+
self.readData = data;
147+
[self.expectation fulfill];
148+
}
149+
150+
- (void)socketDidDisconnect:(GCDAsyncSocket *)socket withError:(NSError *)error;
151+
{
152+
NSLog(@"[Server] Closed connection: %@", error);
153+
}
154+
85155

86156

87157
@end

0 commit comments

Comments
 (0)