Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When token fetch fails, return the failure reason to the client. #14014

Merged
merged 3 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions FirebaseMessaging/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Unreleased
- [fixed] Improve token-fetch failure logging with detailed error info. (#13997).

# 11.0.0
- [fixed] Completed Messaging's transition to NSSecureCoding (#12343).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ - (void)handleResponseWithData:(NSData *)data
FIRMessagingLoggerDebug(kFIRMessagingMessageCodeInternal001, @"%@", failureReason);
responseError = [NSError messagingErrorWithCode:kFIRMessagingErrorCodeInvalidIdentity
failureReason:failureReason];
} else {
FIRMessagingLoggerDebug(kFIRMessagingMessageCodeTokenFetchOperationRequestError,
@"Token fetch got an error from server: %@", errorValue);
responseError = [NSError messagingErrorWithCode:kFIRMessagingErrorCodeUnknown
failureReason:errorValue];
}
}
if (!responseError) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ - (void)testThatTokenOperationsAuthHeaderStringMatchesCheckin {

[FIRURLSessionOCMockStub
stubURLSessionDataTaskWithResponse:expectedResponse
body:[self dataForResponseWithValidToken:YES]
body:[self dataForResponseWithValidToken:@""]
error:nil
URLSessionMock:self.URLSessionMock
requestValidationBlock:^BOOL(NSURLRequest *_Nonnull sentRequest) {
Expand Down Expand Up @@ -315,7 +315,7 @@ - (void)testThatOptionsDictionaryIsIncludedWithFetchRequest {

[FIRURLSessionOCMockStub
stubURLSessionDataTaskWithResponse:expectedResponse
body:[self dataForResponseWithValidToken:YES]
body:[self dataForResponseWithValidToken:@""]
error:nil
URLSessionMock:self.URLSessionMock
requestValidationBlock:^BOOL(NSURLRequest *_Nonnull sentRequest) {
Expand Down Expand Up @@ -368,7 +368,7 @@ - (void)testServerResetCommand {

[FIRURLSessionOCMockStub
stubURLSessionDataTaskWithResponse:expectedResponse
body:[self dataForResponseWithValidToken:NO]
body:[self dataForResponseWithValidToken:@"RST"]
error:nil
URLSessionMock:self.URLSessionMock
requestValidationBlock:^BOOL(NSURLRequest *_Nonnull sentRequest) {
Expand All @@ -392,6 +392,55 @@ - (void)testServerResetCommand {
}];
}

- (void)testTooManyRegistrationsError {
XCTestExpectation *shouldResetIdentityExpectation =
[self expectationWithDescription:@"When server returns TOO_MANY_REGISTRATIONS, the client "
@"should report the failure reason."];
int64_t tenHoursAgo = FIRMessagingCurrentTimestampInMilliseconds() - 10 * 60 * 60 * 1000;
FIRMessagingCheckinPreferences *checkinPreferences =
[self setCheckinPreferencesWithLastCheckinTime:tenHoursAgo];

FIRMessagingTokenFetchOperation *operation = [[FIRMessagingTokenFetchOperation alloc]
initWithAuthorizedEntity:kAuthorizedEntity
scope:kScope
options:nil
checkinPreferences:checkinPreferences
instanceID:self.instanceID
heartbeatLogger:[[FIRHeartbeatLoggerFake alloc] init]];
NSURL *expectedRequestURL = [NSURL URLWithString:FIRMessagingTokenRegisterServer()];
NSHTTPURLResponse *expectedResponse = [[NSHTTPURLResponse alloc] initWithURL:expectedRequestURL
statusCode:200
HTTPVersion:@"HTTP/1.1"
headerFields:nil];

[FIRURLSessionOCMockStub
stubURLSessionDataTaskWithResponse:expectedResponse
body:[self dataForResponseWithValidToken:
@"TOO_MANY_REGISTRATIONS"]
error:nil
URLSessionMock:self.URLSessionMock
requestValidationBlock:^BOOL(NSURLRequest *_Nonnull sentRequest) {
return YES;
}];

[operation addCompletionHandler:^(FIRMessagingTokenOperationResult result,
NSString *_Nullable token, NSError *_Nullable error) {
XCTAssertEqual(result, FIRMessagingTokenOperationError);
XCTAssertNotNil(error);
XCTAssertEqual(error.code, kFIRMessagingErrorCodeUnknown);
XCTAssertEqualObjects(error.localizedFailureReason, @"TOO_MANY_REGISTRATIONS");

[shouldResetIdentityExpectation fulfill];
}];

[operation start];

[self waitForExpectationsWithTimeout:0.25
handler:^(NSError *_Nullable error) {
XCTAssertNil(error.localizedDescription);
}];
}

- (void)testHTTPAuthHeaderGenerationFromCheckin {
FIRMessagingCheckinPreferences *checkinPreferences =
[[FIRMessagingCheckinPreferences alloc] initWithDeviceID:kDeviceID secretToken:kSecretToken];
Expand Down Expand Up @@ -447,7 +496,7 @@ - (void)assertTokenFetchOperationRequestContainsFirebaseUserAgentAndHeartbeatInf

[FIRURLSessionOCMockStub
stubURLSessionDataTaskWithResponse:expectedResponse
body:[self dataForResponseWithValidToken:NO]
body:[self dataForResponseWithValidToken:@"RST"]
error:nil
URLSessionMock:self.URLSessionMock
requestValidationBlock:^BOOL(NSURLRequest *_Nonnull sentRequest) {
Expand All @@ -471,12 +520,12 @@ - (void)assertTokenFetchOperationRequestContainsFirebaseUserAgentAndHeartbeatInf
}];
}

- (NSData *)dataForResponseWithValidToken:(BOOL)validToken {
- (NSData *)dataForResponseWithValidToken:(NSString *)errorCode {
NSString *response;
if (validToken) {
if (errorCode.length == 0) {
response = [NSString stringWithFormat:@"token=%@", kRegistrationToken];
} else {
response = @"Error=RST";
response = [NSString stringWithFormat:@"Error=%@", errorCode];
}
return [response dataUsingEncoding:NSUTF8StringEncoding];
}
Expand Down
Loading