From 56fceab27f43e7f521c7db0bd97ca86e02220c63 Mon Sep 17 00:00:00 2001 From: Kyle Fuller Date: Mon, 7 Oct 2013 22:34:47 +0100 Subject: [PATCH 1/4] OS X doesn't have UIKit --- Tests/OS X Tests/OS X Tests-Prefix.pch | 1 - 1 file changed, 1 deletion(-) diff --git a/Tests/OS X Tests/OS X Tests-Prefix.pch b/Tests/OS X Tests/OS X Tests-Prefix.pch index 3fdee9d..eb2007e 100644 --- a/Tests/OS X Tests/OS X Tests-Prefix.pch +++ b/Tests/OS X Tests/OS X Tests-Prefix.pch @@ -5,6 +5,5 @@ // #ifdef __OBJC__ - #import #import #endif From ee2438b0706101aaef83e441692c1c329c4e8467 Mon Sep 17 00:00:00 2001 From: Kyle Fuller Date: Mon, 7 Oct 2013 22:35:06 +0100 Subject: [PATCH 2/4] Fix a format string issues with the tests with missing argument --- Tests/Tests/AFHTTPSerializationTests.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/Tests/AFHTTPSerializationTests.m b/Tests/Tests/AFHTTPSerializationTests.m index 29174eb..74ec97b 100644 --- a/Tests/Tests/AFHTTPSerializationTests.m +++ b/Tests/Tests/AFHTTPSerializationTests.m @@ -35,7 +35,7 @@ NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:self.baseURL statusCode:statusCode HTTPVersion:@"1.1" headerFields:@{@"Content-Type": @"text/html"}]; AFHTTPResponseSerializer *serializer = [AFHTTPResponseSerializer serializer]; - XCTAssert([serializer.acceptableStatusCodes containsIndex:statusCode], @"Status code %d should be acceptable"); + XCTAssert([serializer.acceptableStatusCodes containsIndex:statusCode], @"Status code %d should be acceptable", statusCode); NSError *error = nil; [serializer validateResponse:response data:[@"text" dataUsingEncoding:NSUTF8StringEncoding] error:&error]; @@ -50,7 +50,7 @@ NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:self.baseURL statusCode:statusCode HTTPVersion:@"1.1" headerFields:@{@"Content-Type": @"text/html"}]; AFHTTPResponseSerializer *serializer = [AFHTTPResponseSerializer serializer]; - XCTAssert(![serializer.acceptableStatusCodes containsIndex:statusCode], @"Status code %d should not be acceptable"); + XCTAssert(![serializer.acceptableStatusCodes containsIndex:statusCode], @"Status code %d should not be acceptable", statusCode); NSError *error = nil; [serializer validateResponse:response data:[@"text" dataUsingEncoding:NSUTF8StringEncoding] error:&error]; From 62209f6c4b1a064796ab7edef38625a9a842e8cf Mon Sep 17 00:00:00 2001 From: Kyle Fuller Date: Mon, 7 Oct 2013 22:55:35 +0100 Subject: [PATCH 3/4] Certificate chain's are stored in the resource inside the bundle --- Tests/Tests/AFSecurityPolicyTests.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/Tests/AFSecurityPolicyTests.m b/Tests/Tests/AFSecurityPolicyTests.m index c4dac7d..7b0cc44 100644 --- a/Tests/Tests/AFSecurityPolicyTests.m +++ b/Tests/Tests/AFSecurityPolicyTests.m @@ -44,14 +44,14 @@ static SecTrustRef AFUTTrustChainForCertsInDirectory(NSString *directoryPath) { } static SecTrustRef AFUTHTTPBinOrgServerTrust() { - NSString *bundlePath = [[NSBundle bundleForClass:[AFSecurityPolicyTests class]] bundlePath]; + NSString *bundlePath = [[NSBundle bundleForClass:[AFSecurityPolicyTests class]] resourcePath]; NSString *serverCertDirectoryPath = [bundlePath stringByAppendingPathComponent:@"HTTPBinOrgServerTrustChain"]; return AFUTTrustChainForCertsInDirectory(serverCertDirectoryPath); } static SecTrustRef AFUTADNNetServerTrust() { - NSString *bundlePath = [[NSBundle bundleForClass:[AFSecurityPolicyTests class]] bundlePath]; + NSString *bundlePath = [[NSBundle bundleForClass:[AFSecurityPolicyTests class]] resourcePath]; NSString *serverCertDirectoryPath = [bundlePath stringByAppendingPathComponent:@"ADNNetServerTrustChain"]; return AFUTTrustChainForCertsInDirectory(serverCertDirectoryPath); From 7137d69d3315097174ea0b7d4240caf421ea1357 Mon Sep 17 00:00:00 2001 From: Kyle Fuller Date: Mon, 7 Oct 2013 22:55:54 +0100 Subject: [PATCH 4/4] Fix multiple memory leaks in AFSecurityPolicyTests --- Tests/Tests/AFSecurityPolicyTests.m | 37 ++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/Tests/Tests/AFSecurityPolicyTests.m b/Tests/Tests/AFSecurityPolicyTests.m index 7b0cc44..6a3f8bd 100644 --- a/Tests/Tests/AFSecurityPolicyTests.m +++ b/Tests/Tests/AFSecurityPolicyTests.m @@ -39,6 +39,7 @@ static SecTrustRef AFUTTrustChainForCertsInDirectory(NSString *directoryPath) { SecPolicyRef policy = SecPolicyCreateBasicX509(); SecTrustRef trust = NULL; SecTrustCreateWithCertificates((__bridge CFTypeRef)(certs), policy, &trust); + CFRelease(policy); return trust; } @@ -73,27 +74,36 @@ static SecCertificateRef AFUTHTTPBinOrgCertificate() { AFSecurityPolicy *policy = [[AFSecurityPolicy alloc] init]; SecCertificateRef certificate = AFUTHTTPBinOrgCertificate(); [policy setPinnedCertificates:@[(__bridge_transfer NSData *)SecCertificateCopyData(certificate)]]; + CFRelease(certificate); [policy setSSLPinningMode:AFSSLPinningModePublicKey]; - XCTAssert([policy evaluateServerTrust:AFUTHTTPBinOrgServerTrust()], @"HTTPBin.org Public Key Pinning Mode Failed"); + SecTrustRef trust = AFUTHTTPBinOrgServerTrust(); + XCTAssert([policy evaluateServerTrust:trust], @"HTTPBin.org Public Key Pinning Mode Failed"); + CFRelease(trust); } - (void)testCertificatePinningIsEnforcedForHTTPBinOrgPinnedCertificateAgainstHTTPBinOrgServerTrust { AFSecurityPolicy *policy = [[AFSecurityPolicy alloc] init]; SecCertificateRef certificate = AFUTHTTPBinOrgCertificate(); [policy setPinnedCertificates:@[(__bridge_transfer NSData *)SecCertificateCopyData(certificate)]]; + CFRelease(certificate); [policy setSSLPinningMode:AFSSLPinningModeCertificate]; - XCTAssert([policy evaluateServerTrust:AFUTHTTPBinOrgServerTrust()], @"HTTPBin.org Public Key Pinning Mode Failed"); + SecTrustRef trust = AFUTHTTPBinOrgServerTrust(); + XCTAssert([policy evaluateServerTrust:trust], @"HTTPBin.org Public Key Pinning Mode Failed"); + CFRelease(trust); } - (void)testNoPinningIsEnforcedForHTTPBinOrgPinnedCertificateAgainstHTTPBinOrgServerTrust { AFSecurityPolicy *policy = [[AFSecurityPolicy alloc] init]; SecCertificateRef certificate = AFUTHTTPBinOrgCertificate(); [policy setPinnedCertificates:@[(__bridge_transfer NSData *)SecCertificateCopyData(certificate)]]; + CFRelease(certificate); [policy setSSLPinningMode:AFSSLPinningModeNone]; - XCTAssert([policy evaluateServerTrust:AFUTHTTPBinOrgServerTrust()], @"HTTPBin.org Pinning should not have been enforced"); + SecTrustRef trust = AFUTHTTPBinOrgServerTrust(); + XCTAssert([policy evaluateServerTrust:trust], @"HTTPBin.org Pinning should not have been enforced"); + CFRelease(trust); } - (void)testPublicKeyPinningFailsForHTTPBinOrgIfNoCertificateIsPinned { @@ -101,7 +111,9 @@ static SecCertificateRef AFUTHTTPBinOrgCertificate() { [policy setPinnedCertificates:@[]]; [policy setSSLPinningMode:AFSSLPinningModePublicKey]; - XCTAssert([policy evaluateServerTrust:AFUTHTTPBinOrgServerTrust()] == NO, @"HTTPBin.org Public Key Pinning Should have failed with no pinned certificate"); + SecTrustRef trust = AFUTHTTPBinOrgServerTrust(); + XCTAssert([policy evaluateServerTrust:trust] == NO, @"HTTPBin.org Public Key Pinning Should have failed with no pinned certificate"); + CFRelease(trust); } - (void)testCertificatePinningFailsForHTTPBinOrgIfNoCertificateIsPinned { @@ -109,7 +121,9 @@ static SecCertificateRef AFUTHTTPBinOrgCertificate() { [policy setPinnedCertificates:@[]]; [policy setSSLPinningMode:AFSSLPinningModeCertificate]; - XCTAssert([policy evaluateServerTrust:AFUTHTTPBinOrgServerTrust()] == NO, @"HTTPBin.org Certificate Pinning Should have failed with no pinned certificate"); + SecTrustRef trust = AFUTHTTPBinOrgServerTrust(); + XCTAssert([policy evaluateServerTrust:trust] == NO, @"HTTPBin.org Certificate Pinning Should have failed with no pinned certificate"); + CFRelease(trust); } - (void)testNoPinningIsEnforcedForHTTPBinOrgIfNoCertificateIsPinned { @@ -117,7 +131,9 @@ static SecCertificateRef AFUTHTTPBinOrgCertificate() { [policy setPinnedCertificates:@[]]; [policy setSSLPinningMode:AFSSLPinningModeNone]; - XCTAssert([policy evaluateServerTrust:AFUTHTTPBinOrgServerTrust()], @"HTTPBin.org Pinning should not have been enforced"); + SecTrustRef trust = AFUTHTTPBinOrgServerTrust(); + XCTAssert([policy evaluateServerTrust:trust], @"HTTPBin.org Pinning should not have been enforced"); + CFRelease(trust); } - (void)testPublicKeyPinningForHTTPBinOrgFailsWhenPinnedAgainstADNServerTrust { @@ -125,7 +141,9 @@ static SecCertificateRef AFUTHTTPBinOrgCertificate() { [policy setPinnedCertificates:@[]]; [policy setSSLPinningMode:AFSSLPinningModePublicKey]; - XCTAssert([policy evaluateServerTrust:AFUTADNNetServerTrust()] == NO, @"HTTPBin.org Public Key Pinning Should have failed against ADN"); + SecTrustRef trust = AFUTADNNetServerTrust(); + XCTAssert([policy evaluateServerTrust:trust] == NO, @"HTTPBin.org Public Key Pinning Should have failed against ADN"); + CFRelease(trust); } - (void)testCertificatePinningForHTTPBinOrgFailsWhenPinnedAgainstADNServerTrust { @@ -133,13 +151,16 @@ static SecCertificateRef AFUTHTTPBinOrgCertificate() { [policy setPinnedCertificates:@[]]; [policy setSSLPinningMode:AFSSLPinningModeCertificate]; - XCTAssert([policy evaluateServerTrust:AFUTADNNetServerTrust()] == NO, @"HTTPBin.org Certificate Pinning Should have failed against ADN"); + SecTrustRef trust = AFUTADNNetServerTrust(); + XCTAssert([policy evaluateServerTrust:trust] == NO, @"HTTPBin.org Certificate Pinning Should have failed against ADN"); + CFRelease(trust); } - (void)testDefaultPolicyContainsHTTPBinOrgCertificate { AFSecurityPolicy *policy = [AFSecurityPolicy defaultPolicy]; SecCertificateRef cert = AFUTHTTPBinOrgCertificate(); NSData *certData = (__bridge NSData *)(SecCertificateCopyData(cert)); + CFRelease(cert); NSInteger index = [policy.pinnedCertificates indexOfObjectPassingTest:^BOOL(NSData *data, NSUInteger idx, BOOL *stop) { return [data isEqualToData:certData]; }];