Skip to content

Commit

Permalink
Fix multiple memory leaks in AFSecurityPolicyTests
Browse files Browse the repository at this point in the history
  • Loading branch information
kylef committed Oct 7, 2013
1 parent 62209f6 commit 7137d69
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions Tests/Tests/AFSecurityPolicyTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -73,73 +74,93 @@ - (void)testPublicKeyPinningIsEnforcedForHTTPBinOrgPinnedCertificateAgainstHTTPB
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 {
AFSecurityPolicy *policy = [[AFSecurityPolicy alloc] init];
[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 {
AFSecurityPolicy *policy = [[AFSecurityPolicy alloc] init];
[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 {
AFSecurityPolicy *policy = [[AFSecurityPolicy alloc] init];
[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 {
AFSecurityPolicy *policy = [[AFSecurityPolicy alloc] init];
[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 {
AFSecurityPolicy *policy = [[AFSecurityPolicy alloc] init];
[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];
}];
Expand Down

0 comments on commit 7137d69

Please sign in to comment.