2021-04-21 21:01:11 -04:00
|
|
|
/*
|
|
|
|
|
* Copyright 2020 Google LLC
|
|
|
|
|
*
|
|
|
|
|
* 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>
|
|
|
|
|
|
2021-05-11 11:39:55 -07:00
|
|
|
#import <OCMock/OCMock.h>
|
2021-04-21 21:01:11 -04:00
|
|
|
|
2023-08-09 13:45:38 -04:00
|
|
|
#import "FirebaseAppCheck/Sources/Core/Errors/FIRAppCheckErrorUtil.h"
|
2021-06-11 17:35:37 -04:00
|
|
|
#import "FirebaseAppCheck/Sources/Core/FIRAppCheckToken+Internal.h"
|
2021-04-21 21:01:11 -04:00
|
|
|
#import "FirebaseAppCheck/Sources/Public/FirebaseAppCheck/FIRDeviceCheckProvider.h"
|
|
|
|
|
|
2022-03-22 13:36:33 -04:00
|
|
|
#import "FirebaseCore/Extension/FirebaseCoreInternal.h"
|
2021-04-21 21:01:11 -04:00
|
|
|
|
2023-11-07 18:54:45 -05:00
|
|
|
static NSString *const kAppName = @"test_app_name";
|
|
|
|
|
static NSString *const kAppID = @"test_app_id";
|
|
|
|
|
static NSString *const kAPIKey = @"test_api_key";
|
|
|
|
|
static NSString *const kProjectID = @"test_project_id";
|
|
|
|
|
static NSString *const kProjectNumber = @"123456789";
|
2021-10-13 16:48:17 -04:00
|
|
|
|
2022-08-12 00:49:32 -04:00
|
|
|
FIR_DEVICE_CHECK_PROVIDER_AVAILABILITY
|
|
|
|
|
@interface FIRDeviceCheckProvider (Tests)
|
|
|
|
|
|
2023-11-07 18:54:45 -05:00
|
|
|
- (instancetype)initWithDeviceCheckProvider:(GACDeviceCheckProvider *)deviceCheckProvider;
|
2022-08-12 00:49:32 -04:00
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
FIR_DEVICE_CHECK_PROVIDER_AVAILABILITY
|
2021-04-21 21:01:11 -04:00
|
|
|
@interface FIRDeviceCheckProviderTests : XCTestCase
|
2021-10-13 16:48:17 -04:00
|
|
|
|
2023-11-07 18:54:45 -05:00
|
|
|
@property(nonatomic, copy) NSString *resourceName;
|
|
|
|
|
@property(nonatomic) id deviceCheckProviderMock;
|
2021-04-21 21:01:11 -04:00
|
|
|
@property(nonatomic) FIRDeviceCheckProvider *provider;
|
2021-10-13 16:48:17 -04:00
|
|
|
|
2021-04-21 21:01:11 -04:00
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation FIRDeviceCheckProviderTests
|
|
|
|
|
|
|
|
|
|
- (void)setUp {
|
|
|
|
|
[super setUp];
|
|
|
|
|
|
2023-11-07 18:54:45 -05:00
|
|
|
self.resourceName = [NSString stringWithFormat:@"projects/%@/apps/%@", kProjectID, kAppID];
|
|
|
|
|
self.deviceCheckProviderMock = OCMStrictClassMock([GACDeviceCheckProvider class]);
|
|
|
|
|
self.provider =
|
|
|
|
|
[[FIRDeviceCheckProvider alloc] initWithDeviceCheckProvider:self.deviceCheckProviderMock];
|
2021-04-21 21:01:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)tearDown {
|
|
|
|
|
self.provider = nil;
|
2023-11-07 18:54:45 -05:00
|
|
|
[self.deviceCheckProviderMock stopMocking];
|
|
|
|
|
self.deviceCheckProviderMock = nil;
|
2021-04-21 21:01:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)testInitWithValidApp {
|
2023-11-07 18:54:45 -05:00
|
|
|
FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kAppID GCMSenderID:kProjectNumber];
|
|
|
|
|
options.APIKey = kAPIKey;
|
|
|
|
|
options.projectID = kProjectID;
|
|
|
|
|
FIRApp *app = [[FIRApp alloc] initInstanceWithName:kAppName options:options];
|
|
|
|
|
// The following disables automatic token refresh, which could interfere with tests.
|
|
|
|
|
app.dataCollectionDefaultEnabled = NO;
|
2021-04-21 21:01:11 -04:00
|
|
|
|
|
|
|
|
XCTAssertNotNil([[FIRDeviceCheckProvider alloc] initWithApp:app]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)testInitWithIncompleteApp {
|
2023-11-07 18:54:45 -05:00
|
|
|
FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kAppID GCMSenderID:kProjectNumber];
|
|
|
|
|
options.projectID = kProjectID;
|
|
|
|
|
FIRApp *missingAPIKeyApp = [[FIRApp alloc] initInstanceWithName:kAppName options:options];
|
|
|
|
|
// The following disables automatic token refresh, which could interfere with tests.
|
|
|
|
|
missingAPIKeyApp.dataCollectionDefaultEnabled = NO;
|
2021-04-21 21:01:11 -04:00
|
|
|
|
|
|
|
|
XCTAssertNil([[FIRDeviceCheckProvider alloc] initWithApp:missingAPIKeyApp]);
|
|
|
|
|
|
|
|
|
|
options.projectID = nil;
|
2023-11-07 18:54:45 -05:00
|
|
|
options.APIKey = kAPIKey;
|
|
|
|
|
FIRApp *missingProjectIDApp = [[FIRApp alloc] initInstanceWithName:kAppName options:options];
|
|
|
|
|
// The following disables automatic token refresh, which could interfere with tests.
|
|
|
|
|
missingProjectIDApp.dataCollectionDefaultEnabled = NO;
|
2021-04-21 21:01:11 -04:00
|
|
|
XCTAssertNil([[FIRDeviceCheckProvider alloc] initWithApp:missingProjectIDApp]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)testGetTokenSuccess {
|
2023-11-07 18:54:45 -05:00
|
|
|
// 1. Stub internal DeviceCheck provider.
|
|
|
|
|
GACAppCheckToken *validInternalToken = [[GACAppCheckToken alloc] initWithToken:@"valid_token"
|
|
|
|
|
expirationDate:[NSDate date]
|
|
|
|
|
receivedAtDate:[NSDate date]];
|
|
|
|
|
OCMExpect([self.deviceCheckProviderMock
|
|
|
|
|
getTokenWithCompletion:([OCMArg
|
|
|
|
|
invokeBlockWithArgs:validInternalToken, [NSNull null], nil])]);
|
2023-08-09 13:45:38 -04:00
|
|
|
|
2023-11-07 18:54:45 -05:00
|
|
|
// 2. Validate get token.
|
2021-04-21 21:01:11 -04:00
|
|
|
[self.provider
|
|
|
|
|
getTokenWithCompletion:^(FIRAppCheckToken *_Nullable token, NSError *_Nullable error) {
|
2023-11-07 18:54:45 -05:00
|
|
|
XCTAssertEqualObjects(token.token, validInternalToken.token);
|
|
|
|
|
XCTAssertEqualObjects(token.expirationDate, validInternalToken.expirationDate);
|
|
|
|
|
XCTAssertEqualObjects(token.receivedAtDate, validInternalToken.receivedAtDate);
|
2021-04-21 21:01:11 -04:00
|
|
|
XCTAssertNil(error);
|
|
|
|
|
}];
|
|
|
|
|
|
2023-11-07 18:54:45 -05:00
|
|
|
// 3. Verify mock DeviceCheck provider.
|
|
|
|
|
OCMVerifyAll(self.deviceCheckProviderMock);
|
2021-04-21 21:01:11 -04:00
|
|
|
}
|
|
|
|
|
|
2023-11-07 18:54:45 -05:00
|
|
|
- (void)testGetTokenAPIError {
|
|
|
|
|
// 1. Stub internal DeviceCheck provider.
|
|
|
|
|
NSError *expectedError = [NSError errorWithDomain:@"testGetTokenAPIError" code:-1 userInfo:nil];
|
|
|
|
|
OCMExpect([self.deviceCheckProviderMock
|
|
|
|
|
getTokenWithCompletion:([OCMArg invokeBlockWithArgs:[NSNull null], expectedError, nil])]);
|
2023-08-09 13:45:38 -04:00
|
|
|
|
2023-11-07 18:54:45 -05:00
|
|
|
// 2. Validate get token.
|
2023-08-09 13:45:38 -04:00
|
|
|
[self.provider
|
|
|
|
|
getTokenWithCompletion:^(FIRAppCheckToken *_Nullable token, NSError *_Nullable error) {
|
|
|
|
|
XCTAssertNil(token);
|
|
|
|
|
XCTAssertEqualObjects(error, expectedError);
|
|
|
|
|
}];
|
|
|
|
|
|
2023-11-07 18:54:45 -05:00
|
|
|
// 3. Verify mock DeviceCheck provider.
|
|
|
|
|
OCMVerifyAll(self.deviceCheckProviderMock);
|
2021-04-21 21:01:11 -04:00
|
|
|
}
|
|
|
|
|
|
2024-07-22 17:32:30 +02:00
|
|
|
#pragma mark - Limited-Use Token
|
|
|
|
|
|
|
|
|
|
- (void)testGetLimitedUseTokenSuccess {
|
|
|
|
|
GACAppCheckToken *validInternalToken = [[GACAppCheckToken alloc] initWithToken:@"TEST_ValidToken"
|
|
|
|
|
expirationDate:[NSDate date]
|
|
|
|
|
receivedAtDate:[NSDate date]];
|
|
|
|
|
OCMExpect([self.deviceCheckProviderMock
|
|
|
|
|
getLimitedUseTokenWithCompletion:([OCMArg invokeBlockWithArgs:validInternalToken,
|
|
|
|
|
[NSNull null], nil])]);
|
|
|
|
|
|
|
|
|
|
[self.provider getLimitedUseTokenWithCompletion:^(FIRAppCheckToken *_Nullable token,
|
|
|
|
|
NSError *_Nullable error) {
|
|
|
|
|
XCTAssertEqualObjects(token.token, validInternalToken.token);
|
|
|
|
|
XCTAssertEqualObjects(token.expirationDate, validInternalToken.expirationDate);
|
|
|
|
|
XCTAssertEqualObjects(token.receivedAtDate, validInternalToken.receivedAtDate);
|
|
|
|
|
XCTAssertNil(error);
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
|
|
OCMVerifyAll(self.deviceCheckProviderMock);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)testGetLimitedUseTokenProviderError {
|
|
|
|
|
NSError *expectedError = [NSError errorWithDomain:@"TEST_LimitedUseToken_Error"
|
|
|
|
|
code:-1
|
|
|
|
|
userInfo:nil];
|
|
|
|
|
OCMExpect([self.deviceCheckProviderMock
|
|
|
|
|
getLimitedUseTokenWithCompletion:([OCMArg invokeBlockWithArgs:[NSNull null], expectedError,
|
|
|
|
|
nil])]);
|
|
|
|
|
|
|
|
|
|
[self.provider getLimitedUseTokenWithCompletion:^(FIRAppCheckToken *_Nullable token,
|
|
|
|
|
NSError *_Nullable error) {
|
|
|
|
|
XCTAssertNil(token);
|
|
|
|
|
XCTAssertIdentical(error, expectedError);
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
|
|
OCMVerifyAll(self.deviceCheckProviderMock);
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-21 21:01:11 -04:00
|
|
|
@end
|