-
Notifications
You must be signed in to change notification settings - Fork 72
/
ConnectionTest.m
executable file
·360 lines (280 loc) · 15.7 KB
/
ConnectionTest.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
//
// ConnectionTest.m
// Marvel
//
// Created by Dan Wood on 11/29/04.
// Copyright (c) 2004 Biophony, LLC. All rights reserved.
//
#import "ConnectionTest.h"
#import "FTPConnection.h"
#import "FileConnection.h"
/* AVAILABLE MACROS
UKPass() Pass always
UKFail() Fail always
UKTrue(condition) Pass if condition is true
UKFalse(condition) Pass if condition is false
UKNil(ref) Pass if ref is nil
UKNotNil(ref) Pass if ref is not nil
UKIntsEqual(a, b) Pass if a == b
UKIntsNotEqual(a, b) Pass if a != b
UKFloatsEqual(a, b, d) Pass if a == b
UKFloatsNotEqual(a, b, d) Pass if a != b
UKObjectsEqual(a, b) Pass if a isEqualTo: b
UKObjectsNotEqual(a, b) Pass if NOT a isEqualTo: b
UKObjectsSame(a, b) Pass if a (address) == b (address)
UKObjectsNotSame(a, b) Pass if a (address) != b (address)
UKStringsEqual(a, b) Pass if a isEqualToString: b
UKStringsNotEqual(a, b) Pass if a NOT isEqualToString: b
UKStringContains(a, b) Pass if a contains b
UKStringDoesNotContain(a, b) Pass if a does NOT contain b
UKRaisesException(exp) Pass if exp raises an exception
UKDoesNotRaiseException(exp) Pass if exp does NOT raise an exception
UKRaisesExceptionNamed(exp, b) Pass if exp raises an exception named b
UKRaisesExceptionClass(exp, b) Pass if exp does NOT raise an exception named b
*/
@implementation ConnectionTest
- (id)init
{
if (self = [super init])
{
myUniqueNumber = (unsigned long) [NSDate timeIntervalSinceReferenceDate];
[self setCallbackDictionary:[NSMutableDictionary dictionary]];
}
return self;
}
- (void)dealloc
{
[self setCallbackDictionary:nil];
[super dealloc];
}
- (NSString *)currentDirectory
{
return myCurrentDirectory;
}
- (void)setCurrentDirectory:(NSString *)aCurrentDirectory
{
[aCurrentDirectory retain];
[myCurrentDirectory release];
myCurrentDirectory = aCurrentDirectory;
}
/*
For all the connection methods, run a standard suite of tests. Test the following methods:
(Need to hook up a delegate to self, somehow put it into a state of what is expected, so we'll know if it's called. If we expect it to be called and it's not, we can have it set an ivar and we'll check.)
What will be tricky will be that this is async. We may need to queue up a bunch of operations, and then wait somehow until they have completed. Maybe make a runloop?
Making connection
changing to directory (good/bad)
getting directory (as expected?)
making directory (good/bad)
making directory with certain permissions
setting permissions (try a couple of times to make sure they actually change) (success/fail)
rename
delete
delete directory
upload file (some known file, like /etc/hosts, or make a new /tmp file with UUID)
.... to directory
resume upload file at offset (supported for all?)
download file
resumeDownload file
cancel transfer
get directory contents
*/
#pragma mark -
#pragma mark Callbacks
/*! These callbacks work by adding their method name to the dictionary so we can tell what has been run.
Temporarily scrunched to one line per method, to make it easy to count and sort.
*/
- (void)connection:(id <AbstractConnectionProtocol>)con didChangeToDirectory:(NSString *)dirPath { [self setCurrentDirectory:dirPath]; NSLog(@"========= CALLBACK: %@", NSStringFromSelector(_cmd)); [myCallbackDictionary setObject:dirPath forKey:NSStringFromSelector(_cmd)]; }
- (void)connection:(id <AbstractConnectionProtocol>)con didConnectToHost:(NSString *)host { NSLog(@"========= CALLBACK: %@", NSStringFromSelector(_cmd)); [myCallbackDictionary setObject:host forKey:NSStringFromSelector(_cmd)]; }
- (void)connection:(id <AbstractConnectionProtocol>)con didCreateDirectory:(NSString *)dirPath { NSLog(@"========= CALLBACK: %@", NSStringFromSelector(_cmd)); [myCallbackDictionary setObject:dirPath forKey:NSStringFromSelector(_cmd)]; }
- (void)connection:(id <AbstractConnectionProtocol>)con didDeleteDirectory:(NSString *)dirPath { NSLog(@"========= CALLBACK: %@", NSStringFromSelector(_cmd)); [myCallbackDictionary setObject:dirPath forKey:NSStringFromSelector(_cmd)]; }
- (void)connection:(id <AbstractConnectionProtocol>)con didDeleteFile:(NSString *)path { NSLog(@"========= CALLBACK: %@", NSStringFromSelector(_cmd)); [myCallbackDictionary setObject:path forKey:NSStringFromSelector(_cmd)]; }
- (void)connection:(id <AbstractConnectionProtocol>)con didDisconnectFromHost:(NSString *)host { NSLog(@"========= CALLBACK: %@", NSStringFromSelector(_cmd)); [myCallbackDictionary setObject:host forKey:NSStringFromSelector(_cmd)]; }
- (void)connection:(id <AbstractConnectionProtocol>)con didReceiveContents:(NSArray *)contents ofDirectory:(NSString *)dirPath { NSLog(@"========= CALLBACK: %@", NSStringFromSelector(_cmd)); [myCallbackDictionary setObject:contents forKey:NSStringFromSelector(_cmd)]; }
- (void)connection:(id <AbstractConnectionProtocol>)con didReceiveError:(NSError *)error { NSLog(@"========= CALLBACK: %@", NSStringFromSelector(_cmd)); [myCallbackDictionary setObject:error forKey:NSStringFromSelector(_cmd)]; }
- (void)connection:(id <AbstractConnectionProtocol>)con didRename:(NSString *)fromPath to:(NSString *)toPath { NSLog(@"========= CALLBACK: %@", NSStringFromSelector(_cmd)); [myCallbackDictionary setObject:[NSString stringWithFormat:@"%@ -> %@",fromPath,toPath] forKey:NSStringFromSelector(_cmd)]; }
- (void)connection:(id <AbstractConnectionProtocol>)con didSetPermissionsForFile:(NSString *)path { NSLog(@"========= CALLBACK: %@", NSStringFromSelector(_cmd)); [myCallbackDictionary setObject:path forKey:NSStringFromSelector(_cmd)]; }
- (void)connection:(id <AbstractConnectionProtocol>)con download:(NSString *)path progressedTo:(NSNumber *)percent { NSLog(@"========= CALLBACK: %@", NSStringFromSelector(_cmd)); [myCallbackDictionary setObject:[NSString stringWithFormat:@"%@: %@%%",path,percent] forKey:NSStringFromSelector(_cmd)]; }
- (void)connection:(id <AbstractConnectionProtocol>)con download:(NSString *)path receivedDataOfLength:(int)length { NSLog(@"========= CALLBACK: %@", NSStringFromSelector(_cmd)); [myCallbackDictionary setObject:[NSString stringWithFormat:@"%@: %d bytes",path,length] forKey:NSStringFromSelector(_cmd)]; }
- (void)connection:(id <AbstractConnectionProtocol>)con downloadDidBegin:(NSString *)remotePath { NSLog(@"========= CALLBACK: %@", NSStringFromSelector(_cmd)); [myCallbackDictionary setObject:remotePath forKey:NSStringFromSelector(_cmd)]; }
- (void)connection:(id <AbstractConnectionProtocol>)con downloadDidFinish:(NSString *)remotePath { NSLog(@"========= CALLBACK: %@", NSStringFromSelector(_cmd)); [myCallbackDictionary setObject:remotePath forKey:NSStringFromSelector(_cmd)]; }
- (NSString *)connection:(id <AbstractConnectionProtocol>)con needsAccountForUsername:(NSString *)username { NSLog(@"========= CALLBACK: %@", NSStringFromSelector(_cmd)); [myCallbackDictionary setObject:username forKey:NSStringFromSelector(_cmd)]; return @"foo"; }
- (void)connection:(id <AbstractConnectionProtocol>)con upload:(NSString *)remotePath progressedTo:(NSNumber *)percent { NSLog(@"========= CALLBACK: %@", NSStringFromSelector(_cmd)); [myCallbackDictionary setObject:[NSString stringWithFormat:@"%@: %@%%",remotePath,percent] forKey:NSStringFromSelector(_cmd)]; }
- (void)connection:(id <AbstractConnectionProtocol>)con upload:(NSString *)remotePath sentDataOfLength:(int)length { NSLog(@"========= CALLBACK: %@", NSStringFromSelector(_cmd)); [myCallbackDictionary setObject:[NSString stringWithFormat:@"%@: %d bytes",remotePath,length] forKey:NSStringFromSelector(_cmd)]; }
- (void)connection:(id <AbstractConnectionProtocol>)con uploadDidBegin:(NSString *)remotePath { NSLog(@"========= CALLBACK: %@", NSStringFromSelector(_cmd)); [myCallbackDictionary setObject:remotePath forKey:NSStringFromSelector(_cmd)]; }
- (void)connection:(id <AbstractConnectionProtocol>)con uploadDidFinish:(NSString *)remotePath { NSLog(@"========= CALLBACK: %@", NSStringFromSelector(_cmd)); [myCallbackDictionary setObject:remotePath forKey:NSStringFromSelector(_cmd)]; }
- (void)connectionDidCancelTransfer:(id <AbstractConnectionProtocol>)con { NSLog(@"========= CALLBACK: %@", NSStringFromSelector(_cmd)); [myCallbackDictionary setObject:[NSNumber numberWithBool:YES] forKey:NSStringFromSelector(_cmd)]; }
- (void)connectionDidSendBadPassword:(id <AbstractConnectionProtocol>)con { NSLog(@"========= CALLBACK: %@", NSStringFromSelector(_cmd)); [myCallbackDictionary setObject:[NSNumber numberWithBool:YES] forKey:NSStringFromSelector(_cmd)]; }
#pragma mark -
#pragma mark Main Test Suite
/*! Do a full test suite
*/
- (void)runTestSuiteWithConnection:(id <AbstractConnectionProtocol>)aConn expectingFailure:(BOOL) inExpectConnectionFailure
{
[aConn setDelegate:self];
BOOL done = NO;
NSString *initialDirectory = nil;
NSDate *dropDeadDate = [NSDate dateWithTimeIntervalSinceNow:120.0];
[aConn connect];
while (!done && NSOrderedAscending == [((NSDate *)[NSDate date]) compare:(NSDate *)dropDeadDate])
{
if ([((NSObject *)aConn) isKindOfClass:[FTPConnection class]])
{
NSLog(@"========= TEST: top of loop, state = %d, queue= %@", [((FTPConnection *)aConn) state], [((FTPConnection *)aConn) queueDescription]);
}
if (0 == [myCallbackDictionary count]) // We don't have any callbacks from the previous iteration
{
NSLog(@"========= TEST: will run NSRunLoop");
BOOL found = [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:30.0]];
//[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:20.0]];
//[[NSRunLoop currentRunLoop] run];
NSLog(@"========= TEST: did run NSRunLoop, found = %d dict = %@", found, [[myCallbackDictionary allKeys] description]);
if (!found)
{
done = true; // nothing found after waiting this long, so we're done.
break;
}
}
id value;
NSDictionary *lastCallbackDictionary = [[myCallbackDictionary copy] autorelease];
[myCallbackDictionary removeAllObjects]; // clean out the original dict for the next pass
if (nil != (value = [lastCallbackDictionary objectForKey:@"connection:didReceiveError:"]) )
{
NSLog(@"========= TEST: Error message: %@", value);
UKTrue (inExpectConnectionFailure); // pass if we did expect an error!
done = YES; // we're done anyhow.
}
else if (nil != (value = [lastCallbackDictionary objectForKey:@"connectionDidSendBadPassword:"]) )
{
UKTrue (inExpectConnectionFailure); // pass if we did expect an error!
done = YES; // we're done anyhow.
}
else if (nil != (value = [lastCallbackDictionary objectForKey:@"connection:didConnectToHost:"]) )
{
NSLog(@"========= TEST: Connected to host: %@", value);
UKFalse (inExpectConnectionFailure); // pass if we did not expect an error
// NEXT TASK: MAKE A NEW DIRECTORY
[aConn createDirectory:[NSString stringWithFormat:@"d%ld", myUniqueNumber]];
}
else if (nil != (value = [lastCallbackDictionary objectForKey:@"connection:didCreateDirectory:"]) )
{
NSLog(@"========= TEST: Created directory called %@", [NSString stringWithFormat:@"d%ld", myUniqueNumber]);
UKPass(); // Got our directory
// NEXT TASK: Get the contents of the current directory
[aConn directoryContents];
}
else if (nil != (value = [lastCallbackDictionary objectForKey:@"connection:didReceiveContents:ofDirectory:"]) )
{
NSLog(@"========= TEST: Got directory contents %@", [value description]);
UKPass(); // Got our directory ... REALY OUGHT TO VERIFY THAT OUR FILE IS IN THERE.
// NEXT TASK: Change to that new directory
[aConn changeToDirectory:[NSString stringWithFormat:@"d%ld", myUniqueNumber]];
}
else if (nil != (value = [lastCallbackDictionary objectForKey:@"connection:didChangeToDirectory:"]) )
{
if (nil == initialDirectory)
{
initialDirectory = [self currentDirectory]; // this will depend on what connection we have
UKPass(); // Got our directory ... REALY OUGHT TO VERIFY THAT OUR FILE IS IN THERE.
}
else // second change; make sure we are within initial directory
{
NSString *expectedNewDirectory = [initialDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"d%ld", myUniqueNumber]];
UKStringsEqual([self currentDirectory], expectedNewDirectory);
}
NSLog(@"========= TEST: Changed to directory %@", [value description]);
// NEXT TASK: Upload a file
[aConn uploadFile:@"/etc/hosts"];
}
else if (nil != (value = [lastCallbackDictionary objectForKey:@"connection:uploadDidFinish:"]) )
{
NSLog(@"========= TEST: Done uploading %@", [value description]);
UKPass(); // Got our directory ... REALY OUGHT TO VERIFY THAT OUR FILE IS IN THERE.
done = YES; // done for now ... we don't have any more tests.
}
/*
changing to directory (good/bad)
getting directory (as expected?)
making directory (good/bad)
making directory with certain permissions
setting permissions (try a couple of times to make sure they actually change) (success/fail)
rename
delete
delete directory
upload file (some known file, like /etc/hosts, or make a new /tmp file with UUID)
.... to directory
resume upload file at offset (supported for all?)
download file
resumeDownload file
cancel transfer
get directory contents
*/
}
[aConn disconnect];
NSLog(@"========= TEST: Disconnected.");
if (! (NSOrderedAscending == [((NSDate *)[NSDate date]) compare:dropDeadDate]) )
{
UKFail(); // Ran out of time!
}
}
#pragma mark -
#pragma mark File tests
/*! Test a file connection. Since there is no connection, we can't test for a bad file connection
*/
- (void)testFileConnection
{
FileConnection *fc = [FileConnection connection];
[self runTestSuiteWithConnection:fc expectingFailure:NO];
}
#pragma mark -
#pragma mark FTP tests
- (void)testRealFTPConnection
{
NSDictionary *environment = [[NSProcessInfo processInfo] environment];
NSString *password = [environment objectForKey:@"biophonyTestPassword"];
FTPConnection *c = [FTPConnection connectionToHost:@"test.biophony.com" port:nil username:@"test" password:password];
[self runTestSuiteWithConnection:c expectingFailure:NO];
}
- (void) testQuotesScan
{
FTPConnection *c = [FTPConnection connectionToHost:@"bogus" port:nil username:@"test" password:@"nothing"];
UKNil([c scanBetweenQuotes:@"hey there we're the monkeys"]);
UKNil([c scanBetweenQuotes:@"This has one \" quote mark"]);
UKStringsEqual([c scanBetweenQuotes:@"empty \"\" quoted string"], @"");
UKStringsEqual([c scanBetweenQuotes:@"257 \"/somethingee\" created"], @"/somethingee");
UKStringsEqual([c scanBetweenQuotes:@"257 \"/he said \"\"yo\"\" to me\" created"], @"/he said \"yo\" to me");
}
/*
- (void)testBadAccountFTPConnection
{
FTPConnection *c = [FTPConnection connectionToHost:@"test.biophony.com" port:nil username:@"somebodyelse" password:@"mypassword"];
[self runTestSuiteWithConnection:c expectingFailure:YES];
}
- (void)testConnectionRefusedFTPConnection
{
FTPConnection *c = [FTPConnection connectionToHost:@"vorlon.karelia.com" port:nil username:@"somebodyelse" password:@"mypassword"];
[self runTestSuiteWithConnection:c expectingFailure:YES];
}
- (void)testHangingFTPConnection
{
FTPConnection *c = [FTPConnection connectionToHost:@"ftp.zocalo.net" port:nil username:@"somebodyelse" password:@"mypassword"];
[self runTestSuiteWithConnection:c expectingFailure:YES];
}
- (void)testNoHostFTPConnection
{
FTPConnection *c = [FTPConnection connectionToHost:@"fdjskalrejwfje.com" port:nil username:@"somebodyelse" password:@"mypassword"];
[self runTestSuiteWithConnection:c expectingFailure:YES];
}
*/
#pragma mark -
#pragma mark Private Support
- (NSMutableDictionary *)callbackDictionary
{
return myCallbackDictionary;
}
- (void)setCallbackDictionary:(NSMutableDictionary *)aCallbackDictionary
{
[aCallbackDictionary retain];
[myCallbackDictionary release];
myCallbackDictionary = aCallbackDictionary;
}
@end