Skip to content

FIS: stored registration error timeout #4032

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

Merged
merged 3 commits into from
Oct 10, 2019
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
2 changes: 2 additions & 0 deletions FirebaseInstallations/Source/Library/FIRInstallationsItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,11 @@ NS_ASSUME_NONNULL_BEGIN
/**
* Updates `registrationStatus` and `registrationError` accordingly.
* @param error The error for the Installation Registration API request.
* @param date The date when the error occurred.
* @param registrationParameters The parameters used for the Installation Registration API request.
*/
- (void)updateWithRegistrationError:(NSError *)error
date:(NSDate *)date
registrationParameters:
(FIRInstallationsStoredRegistrationParameters *)registrationParameters;

Expand Down
2 changes: 2 additions & 0 deletions FirebaseInstallations/Source/Library/FIRInstallationsItem.m
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,13 @@ + (NSString *)base64URLEncodedStringWithData:(NSData *)data {
}

- (void)updateWithRegistrationError:(NSError *)error
date:(NSDate *)date
registrationParameters:
(FIRInstallationsStoredRegistrationParameters *)registrationParameters {
self.registrationStatus = FIRInstallationStatusRegistrationFailed;
self.registrationError = [[FIRInstallationsStoredRegistrationError alloc]
initWithRegistrationParameters:registrationParameters
date:date
APIError:error];
self.authToken = nil;
self.refreshToken = nil;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@

NSTimeInterval const kFIRInstallationsTokenExpirationThreshold = 60 * 60; // 1 hour.

NSTimeInterval const kFIRInstallationsRegistrationErrorTimeout = 24 * 60 * 60; // 1 day.

@interface FIRInstallationsIDController ()
@property(nonatomic, readonly) NSString *appID;
@property(nonatomic, readonly) NSString *appName;
Expand Down Expand Up @@ -147,6 +149,7 @@ - (instancetype)initWithGoogleAppID:(NSString *)appID
// Validate if a previous registration attempt failed with an error requiring Firebase
// configuration changes.
if (installation.registrationStatus == FIRInstallationStatusRegistrationFailed &&
[self isRegistrationErrorWithDateUpToDate:installation.registrationError.date] &&
[self areInstallationRegistrationParametersEqualToCurrent:
installation.registrationError.registrationParameters]) {
return installation.registrationError.APIError;
Expand Down Expand Up @@ -226,6 +229,11 @@ - (BOOL)areInstallationRegistrationParametersEqualToCurrent:
(parameters.projectID == projectID || [parameters.projectID isEqual:projectID]);
}

- (BOOL)isRegistrationErrorWithDateUpToDate:(NSDate *)errorDate {
return errorDate != nil &&
-[errorDate timeIntervalSinceNow] <= kFIRInstallationsRegistrationErrorTimeout;
}

#pragma mark - FID registration

- (FBLPromise<FIRInstallationsItem *> *)registerInstallationIfNeeded:
Expand Down Expand Up @@ -274,6 +282,7 @@ - (BOOL)areInstallationRegistrationParametersEqualToCurrent:

FIRInstallationsItem *failedInstallation = [installation copy];
[failedInstallation updateWithRegistrationError:error
date:[NSDate date]
registrationParameters:[self currentRegistrationParameters]];

// Save the error and then fail with the API error.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ NS_ASSUME_NONNULL_BEGIN
@interface FIRInstallationsStoredRegistrationError : NSObject <NSSecureCoding>

@property(nonatomic, readonly) FIRInstallationsStoredRegistrationParameters *registrationParameters;
@property(nonatomic, readonly) NSDate *date;
@property(nonatomic, readonly) NSError *APIError;

/// The version of local storage.
@property(nonatomic, readonly) NSInteger storageVersion;

- (instancetype)initWithRegistrationParameters:
(FIRInstallationsStoredRegistrationParameters *)registrationParameters
date:(NSDate *)date
APIError:(NSError *)error;

@end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,20 @@
@"registrationParameters";
NSString *const kFIRInstallationsStoredRegistrationErrorAPIErrorKey = @"APIError";
NSString *const kFIRInstallationsStoredRegistrationErrorStorageVersionKey = @"storageVersion";
NSString *const kFIRInstallationsStoredRegistrationErrorDateKey = @"date";

NSInteger const kFIRInstallationsStoredRegistrationErrorStorageVersion = 1;

@implementation FIRInstallationsStoredRegistrationError

- (instancetype)initWithRegistrationParameters:
(FIRInstallationsStoredRegistrationParameters *)registrationParameters
date:(NSDate *)date
APIError:(NSError *)error {
self = [super init];
if (self) {
_registrationParameters = registrationParameters;
_date = date;
_APIError = error;
}
return self;
Expand All @@ -54,6 +57,7 @@ + (BOOL)supportsSecureCoding {
- (void)encodeWithCoder:(nonnull NSCoder *)coder {
[coder encodeObject:self.registrationParameters
forKey:kFIRInstallationsStoredRegistrationErrorRegistrationParametersKey];
[coder encodeObject:self.date forKey:kFIRInstallationsStoredRegistrationErrorDateKey];
[coder encodeObject:self.APIError forKey:kFIRInstallationsStoredRegistrationErrorAPIErrorKey];
[coder encodeInteger:kFIRInstallationsStoredRegistrationErrorStorageVersion
forKey:kFIRInstallationsStoredRegistrationErrorStorageVersionKey];
Expand All @@ -73,6 +77,8 @@ - (nullable instancetype)initWithCoder:(nonnull NSCoder *)coder {
FIRInstallationsStoredRegistrationParameters *registrationParameters =
[coder decodeObjectOfClass:[FIRInstallationsStoredRegistrationParameters class]
forKey:kFIRInstallationsStoredRegistrationErrorRegistrationParametersKey];
NSDate *date = [coder decodeObjectOfClass:[NSDate class]
forKey:kFIRInstallationsStoredRegistrationErrorDateKey];

NSSet<Class> *allowedErrorClasses =
[NSSet setWithArray:@ [[FIRInstallationsHTTPError class], [NSError class]]];
Expand All @@ -87,7 +93,7 @@ - (nullable instancetype)initWithCoder:(nonnull NSCoder *)coder {
return nil;
}

return [self initWithRegistrationParameters:registrationParameters APIError:APIError];
return [self initWithRegistrationParameters:registrationParameters date:date APIError:APIError];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#import <OCMock/OCMock.h>
#import "FBLPromise+Testing.h"
#import "FIRInstallationsItem+Tests.h"
#import "XCTestCase+DateAsserts.h"

#import "FIRInstallationsAPIService.h"
#import "FIRInstallationsErrorUtil.h"
Expand Down Expand Up @@ -405,16 +406,6 @@ - (NSURLResponse *)responseWithStatusCode:(NSUInteger)statusCode {
return response;
}

- (void)assertDate:(NSDate *)date
isApproximatelyEqualCurrentPlusTimeInterval:(NSTimeInterval)timeInterval {
NSDate *expectedDate = [NSDate dateWithTimeIntervalSinceNow:timeInterval];

NSTimeInterval precision = 10;
XCTAssert(ABS([date timeIntervalSinceDate:expectedDate]) <= precision,
@"date: %@ is not equal to expected %@ with precision %f - %@", date, expectedDate,
precision, self.name);
}

- (id)refreshTokenRequestValidationArgWithInstallation:(FIRInstallationsItem *)installation {
return [OCMArg checkWithBlock:^BOOL(NSURLRequest *request) {
XCTAssertEqualObjects(request.HTTPMethod, @"POST");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#import "FBLPromise+Testing.h"
#import "FIRInstallationsErrorUtil+Tests.h"
#import "FIRInstallationsItem+Tests.h"
#import "XCTestCase+DateAsserts.h"

#import "FIRInstallations.h"
#import "FIRInstallationsAPIService.h"
Expand Down Expand Up @@ -1023,6 +1024,9 @@ - (void)testRegisterInstallation_WhenServerRespondsWith400_ThenErrorStoredAndRet
XCTAssertEqualObjects(
installation.registrationError.registrationParameters.projectID,
self.projectID);
XCTAssertNotNil(installation.registrationError.date);
[self assertDate:installation.registrationError.date
isApproximatelyEqualCurrentPlusTimeInterval:0];
return YES;
}]])
.andReturn([FBLPromise resolvedWith:[NSNull null]]);
Expand Down Expand Up @@ -1104,6 +1108,47 @@ - (void)testGetInstallation_WhenRegistrationErrorStoredAndConfigurationIsTheSame
OCMVerifyAll(self.mockAPIService);
}

- (void)testGetInstallation_WhenStoredRegistrationErrorIsOutdated_ThenSendsAPIRequest {
__block FBLPromise<FIRInstallationsItem *> *storedInstallationPromise;
OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
.andDo(^(NSInvocation *invocation) {
[invocation setReturnValue:&storedInstallationPromise];
});

// 1.1. Expect installation to be requested from the store.
NSDate *date25HoursAgo = [NSDate dateWithTimeIntervalSinceNow:-25 * 60 * 60];
FIRInstallationsItem *storedInstallation =
[self createFailedToRegisterInstallationWithParameters:[self currentRegistrationParameters]
date:date25HoursAgo];
storedInstallationPromise = [FBLPromise resolvedWith:storedInstallation];

// 1.2. Expect registration API request to be sent.
FIRInstallationsItem *registeredInstallation =
[FIRInstallationsItem createRegisteredInstallationItem];
OCMExpect([self.mockAPIService registerInstallation:storedInstallation])
.andReturn([FBLPromise resolvedWith:registeredInstallation]);

// 1.3. Expect registered Installation to be stored.
OCMExpect([self.mockInstallationsStore saveInstallation:[OCMArg checkWithBlock:^BOOL(id obj) {
XCTAssertEqualObjects(obj, registeredInstallation);
storedInstallationPromise =
[FBLPromise resolvedWith:obj];
return YES;
}]])
.andReturn([FBLPromise resolvedWith:[NSNull null]]);

// 2. Request Installation.
FBLPromise<FIRInstallationsItem *> *promise = [self.controller getInstallationItem];
XCTAssert(FBLWaitForPromisesWithTimeout(0.5));

// 3. Check.
XCTAssertEqualObjects(promise.value.identifier, registeredInstallation.identifier);
XCTAssertNil(promise.error);

OCMVerifyAll(self.mockInstallationsStore);
OCMVerifyAll(self.mockAPIService);
}

#pragma mark - Helpers

- (void)expectInstallationsStoreGetInstallationNotFound {
Expand Down Expand Up @@ -1139,14 +1184,22 @@ - (XCTestExpectation *)installationIDDidChangeNotificationExpectation {
}

- (FIRInstallationsItem *)createFailedToRegisterInstallationWithParameters:
(FIRInstallationsStoredRegistrationParameters *)registrationParameters {
(FIRInstallationsStoredRegistrationParameters *)registrationParameters
date:(NSDate *)date {
FIRInstallationsStoredRegistrationError *error = [[FIRInstallationsStoredRegistrationError alloc]
initWithRegistrationParameters:registrationParameters
date:date
APIError:[FIRInstallationsErrorUtil APIErrorWithHTTPCode:400]];
FIRInstallationsItem *installation = [FIRInstallationsItem createWithRegistrationFailure:error];
return installation;
}

- (FIRInstallationsItem *)createFailedToRegisterInstallationWithParameters:
(FIRInstallationsStoredRegistrationParameters *)registrationParameters {
return [self createFailedToRegisterInstallationWithParameters:registrationParameters
date:[NSDate date]];
}

- (FIRInstallationsStoredRegistrationParameters *)currentRegistrationParameters {
return [[FIRInstallationsStoredRegistrationParameters alloc] initWithAPIKey:self.APIKey
projectID:self.projectID];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ - (void)testItemArchivingUnarchiving {
XCTAssertEqual(unarchivedItem.registrationStatus, item.registrationStatus);

XCTAssertEqualObjects(unarchivedItem.registrationError.APIError, item.registrationError.APIError);
XCTAssertEqualObjects(unarchivedItem.registrationError.date, item.registrationError.date);
XCTAssertEqualObjects(unarchivedItem.registrationError.registrationParameters.APIKey,
item.registrationError.registrationParameters.APIKey);
XCTAssertEqualObjects(unarchivedItem.registrationError.registrationParameters.projectID,
Expand All @@ -76,6 +77,7 @@ - (FIRInstallationsStoredRegistrationError *)createRegistrationError {
userInfo:@{NSLocalizedFailureReasonErrorKey : @"value"}];
FIRInstallationsStoredRegistrationError *registrationError =
[[FIRInstallationsStoredRegistrationError alloc] initWithRegistrationParameters:params
date:[NSDate date]
APIError:error];
XCTAssertEqualObjects(registrationError.APIError, error);
XCTAssertEqualObjects(registrationError.registrationParameters, params);
Expand Down
1 change: 1 addition & 0 deletions FirebaseInstallations/Source/Tests/Utils/FIRTestKeychain.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import <Foundation/Foundation.h>
#import <Security/Security.h>

Expand Down
28 changes: 28 additions & 0 deletions FirebaseInstallations/Source/Tests/Utils/XCTestCase+DateAsserts.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2019 Google
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import <XCTest/XCTest.h>

NS_ASSUME_NONNULL_BEGIN

@interface XCTestCase (FIRTestsDateUtils)

- (void)assertDate:(NSDate *)date
isApproximatelyEqualCurrentPlusTimeInterval:(NSTimeInterval)timeInterval;

@end

NS_ASSUME_NONNULL_END
31 changes: 31 additions & 0 deletions FirebaseInstallations/Source/Tests/Utils/XCTestCase+DateAsserts.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2019 Google
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import "XCTestCase+DateAsserts.h"

@implementation XCTestCase (FIRTestsDateUtils)

- (void)assertDate:(NSDate *)date
isApproximatelyEqualCurrentPlusTimeInterval:(NSTimeInterval)timeInterval {
NSDate *expectedDate = [NSDate dateWithTimeIntervalSinceNow:timeInterval];

NSTimeInterval precision = 10;
XCTAssert(ABS([date timeIntervalSinceDate:expectedDate]) <= precision,
@"date: %@ is not equal to expected %@ with precision %f - %@", date, expectedDate,
precision, self.name);
}

@end