Skip to content

Commit 8a99f2d

Browse files
committed
Added test demonstrating failure for PK
1 parent 439e67f commit 8a99f2d

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

Tests/Tests/AFHTTPSessionManagerTests.m

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,15 +511,39 @@ - (void)testHiddenBasicAuthentication {
511511

512512
# pragma mark - Server Trust
513513

514-
- (void)testInvalidServerTrustProducesCorrectError {
514+
- (void)testInvalidServerTrustProducesCorrectErrorForCertificatePinning {
515515
__weak XCTestExpectation *expectation = [self expectationWithDescription:@"Request should fail"];
516516
NSURL *googleCertificateURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"google.com" withExtension:@"cer"];
517517
NSData *googleCertificateData = [NSData dataWithContentsOfURL:googleCertificateURL];
518518
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:@"https://apple.com/"]];
519519
[manager setResponseSerializer:[AFHTTPResponseSerializer serializer]];
520520
manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate withPinnedCertificates:[NSSet setWithObject:googleCertificateData]];
521521
[manager
522-
GET:@"AFNetworking/AFNetworking"
522+
GET:@""
523+
parameters:nil
524+
progress:nil
525+
success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
526+
XCTFail(@"Request should fail");
527+
[expectation fulfill];
528+
}
529+
failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
530+
XCTAssertEqualObjects(error.domain, NSURLErrorDomain);
531+
XCTAssertEqual(error.code, NSURLErrorServerCertificateUntrusted);
532+
[expectation fulfill];
533+
}];
534+
[self waitForExpectationsWithCommonTimeoutUsingHandler:nil];
535+
[manager invalidateSessionCancelingTasks:YES];
536+
}
537+
538+
- (void)testInvalidServerTrustProducesCorrectErrorForPublicKeyPinning {
539+
__weak XCTestExpectation *expectation = [self expectationWithDescription:@"Request should fail"];
540+
NSURL *googleCertificateURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"google.com" withExtension:@"cer"];
541+
NSData *googleCertificateData = [NSData dataWithContentsOfURL:googleCertificateURL];
542+
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:@"https://apple.com/"]];
543+
[manager setResponseSerializer:[AFHTTPResponseSerializer serializer]];
544+
manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModePublicKey withPinnedCertificates:[NSSet setWithObject:googleCertificateData]];
545+
[manager
546+
GET:@""
523547
parameters:nil
524548
progress:nil
525549
success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {

0 commit comments

Comments
 (0)