Ben Copsey

Prevent delegate auth from being used when running on main thread

Added a test for same
@@ -1453,6 +1453,11 @@ static NSRecursiveLock *delegateAuthenticationLock = nil; @@ -1453,6 +1453,11 @@ static NSRecursiveLock *delegateAuthenticationLock = nil;
1453 1453
1454 - (BOOL)askDelegateForProxyCredentials 1454 - (BOOL)askDelegateForProxyCredentials
1455 { 1455 {
  1456 + // Can't use delegate authentication when running on the main thread
  1457 + if ([NSThread isMainThread]) {
  1458 + return NO;
  1459 + }
  1460 +
1456 // If we have a delegate, we'll see if it can handle proxyAuthenticationNeededForRequest:. 1461 // If we have a delegate, we'll see if it can handle proxyAuthenticationNeededForRequest:.
1457 // Otherwise, we'll try the queue (if this request is part of one) and it will pass the message on to its own delegate 1462 // Otherwise, we'll try the queue (if this request is part of one) and it will pass the message on to its own delegate
1458 id authenticationDelegate = [self delegate]; 1463 id authenticationDelegate = [self delegate];
@@ -1635,6 +1640,11 @@ static NSRecursiveLock *delegateAuthenticationLock = nil; @@ -1635,6 +1640,11 @@ static NSRecursiveLock *delegateAuthenticationLock = nil;
1635 1640
1636 - (BOOL)askDelegateForCredentials 1641 - (BOOL)askDelegateForCredentials
1637 { 1642 {
  1643 + // Can't use delegate authentication when running on the main thread
  1644 + if ([NSThread isMainThread]) {
  1645 + return NO;
  1646 + }
  1647 +
1638 // If we have a delegate, we'll see if it can handle proxyAuthenticationNeededForRequest:. 1648 // If we have a delegate, we'll see if it can handle proxyAuthenticationNeededForRequest:.
1639 // Otherwise, we'll try the queue (if this request is part of one) and it will pass the message on to its own delegate 1649 // Otherwise, we'll try the queue (if this request is part of one) and it will pass the message on to its own delegate
1640 id authenticationDelegate = [self delegate]; 1650 id authenticationDelegate = [self delegate];
@@ -39,4 +39,5 @@ @@ -39,4 +39,5 @@
39 - (void)testTimeOutWithoutDownloadDelegate; 39 - (void)testTimeOutWithoutDownloadDelegate;
40 - (void)testThrottlingDownloadBandwidth; 40 - (void)testThrottlingDownloadBandwidth;
41 - (void)testThrottlingUploadBandwidth; 41 - (void)testThrottlingUploadBandwidth;
  42 +- (void)testMainThreadDelegateAuthenticationFailure;
42 @end 43 @end
@@ -857,6 +857,28 @@ @@ -857,6 +857,28 @@
857 857
858 } 858 }
859 859
  860 +- (void)authenticationNeededForRequest:(ASIHTTPRequest *)request
  861 +{
  862 + GHAssertTrue(NO,@"Delegate asked for authentication when running on the main thread");
  863 +}
  864 +
  865 +- (void)testMainThreadDelegateAuthenticationFailure
  866 +{
  867 + [ASIHTTPRequest clearSession];
  868 + //GHUnit will not run this function on the main thread, so we'll need to move it there
  869 + [self performSelectorOnMainThread:@selector(fetchOnMainThread) withObject:nil waitUntilDone:YES];
  870 +
  871 +}
  872 +
  873 +- (void)fetchOnMainThread
  874 +{
  875 + // Ensure the delegate is not called when we are running on the main thread
  876 + ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/basic-authentication"]];
  877 + [request setDelegate:self];
  878 + [request start];
  879 + GHAssertNotNil([request error],@"Failed to generate an authentication error");
  880 +}
  881 +
860 @end 882 @end
861 883
862 884