Ben Copsey

Fix crash if you tried to remove non-existent credentials from the keychain store

@@ -2279,15 +2279,19 @@ static BOOL isiPhoneOS2; @@ -2279,15 +2279,19 @@ static BOOL isiPhoneOS2;
2279 + (void)removeCredentialsForHost:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm 2279 + (void)removeCredentialsForHost:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm
2280 { 2280 {
2281 NSURLProtectionSpace *protectionSpace = [[[NSURLProtectionSpace alloc] initWithHost:host port:port protocol:protocol realm:realm authenticationMethod:NSURLAuthenticationMethodDefault] autorelease]; 2281 NSURLProtectionSpace *protectionSpace = [[[NSURLProtectionSpace alloc] initWithHost:host port:port protocol:protocol realm:realm authenticationMethod:NSURLAuthenticationMethodDefault] autorelease];
2282 - NSURLCredentialStorage *storage = [NSURLCredentialStorage sharedCredentialStorage]; 2282 + NSURLCredential *credential = [[NSURLCredentialStorage sharedCredentialStorage] defaultCredentialForProtectionSpace:protectionSpace];
2283 - [storage removeCredential:[storage defaultCredentialForProtectionSpace:protectionSpace] forProtectionSpace:protectionSpace]; 2283 + if (credential) {
  2284 + [[NSURLCredentialStorage sharedCredentialStorage] removeCredential:credential forProtectionSpace:protectionSpace];
  2285 + }
2284 } 2286 }
2285 2287
2286 + (void)removeCredentialsForProxy:(NSString *)host port:(int)port realm:(NSString *)realm 2288 + (void)removeCredentialsForProxy:(NSString *)host port:(int)port realm:(NSString *)realm
2287 { 2289 {
2288 NSURLProtectionSpace *protectionSpace = [[[NSURLProtectionSpace alloc] initWithProxyHost:host port:port type:NSURLProtectionSpaceHTTPProxy realm:realm authenticationMethod:NSURLAuthenticationMethodDefault] autorelease]; 2290 NSURLProtectionSpace *protectionSpace = [[[NSURLProtectionSpace alloc] initWithProxyHost:host port:port type:NSURLProtectionSpaceHTTPProxy realm:realm authenticationMethod:NSURLAuthenticationMethodDefault] autorelease];
2289 - NSURLCredentialStorage *storage = [NSURLCredentialStorage sharedCredentialStorage]; 2291 + NSURLCredential *credential = [[NSURLCredentialStorage sharedCredentialStorage] defaultCredentialForProtectionSpace:protectionSpace];
2290 - [storage removeCredential:[storage defaultCredentialForProtectionSpace:protectionSpace] forProtectionSpace:protectionSpace]; 2292 + if (credential) {
  2293 + [[NSURLCredentialStorage sharedCredentialStorage] removeCredential:credential forProtectionSpace:protectionSpace];
  2294 + }
2291 } 2295 }
2292 2296
2293 2297
@@ -23,6 +23,7 @@ @@ -23,6 +23,7 @@
23 - (void)testDownloadProgress; 23 - (void)testDownloadProgress;
24 - (void)testUploadProgress; 24 - (void)testUploadProgress;
25 - (void)testCookies; 25 - (void)testCookies;
  26 +- (void)testRemoveCredentialsFromKeychain;
26 - (void)testBasicAuthentication; 27 - (void)testBasicAuthentication;
27 - (void)testDigestAuthentication; 28 - (void)testDigestAuthentication;
28 - (void)testNTLMHandshake; 29 - (void)testNTLMHandshake;
@@ -40,4 +41,5 @@ @@ -40,4 +41,5 @@
40 - (void)testThrottlingDownloadBandwidth; 41 - (void)testThrottlingDownloadBandwidth;
41 - (void)testThrottlingUploadBandwidth; 42 - (void)testThrottlingUploadBandwidth;
42 - (void)testMainThreadDelegateAuthenticationFailure; 43 - (void)testMainThreadDelegateAuthenticationFailure;
  44 +
43 @end 45 @end
@@ -461,6 +461,14 @@ @@ -461,6 +461,14 @@
461 GHAssertTrue(success,@"Cookie presented to the server when it should have been removed"); 461 GHAssertTrue(success,@"Cookie presented to the server when it should have been removed");
462 } 462 }
463 463
  464 +// Test fix for a crash if you tried to remove credentials that didn't exist
  465 +- (void)testRemoveCredentialsFromKeychain
  466 +{
  467 + [ASIHTTPRequest removeCredentialsForHost:@"apple.com" port:0 protocol:@"http" realm:@"Nothing to see here"];
  468 + [ASIHTTPRequest removeCredentialsForProxy:@"apple.com" port:0 realm:@"Nothing to see here"];
  469 +
  470 +}
  471 +
464 472
465 - (void)testBasicAuthentication 473 - (void)testBasicAuthentication
466 { 474 {