forked from google/app-check
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
GACAppCheckTokenResult
type (google#18)
- Loading branch information
1 parent
961b37a
commit f191868
Showing
5 changed files
with
188 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2023 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 "AppCheckCore/Sources/Public/AppCheckCore/GACAppCheckTokenResult.h" | ||
|
||
#import "AppCheckCore/Sources/Public/AppCheckCore/GACAppCheckToken.h" | ||
|
||
/// Placeholder value that indicates failure. | ||
/// This value is `{"error":"UNKNOWN_ERROR"}` encoded as base64. | ||
static NSString *const kPlaceholderTokenValue = @"eyJlcnJvciI6IlVOS05PV05fRVJST1IifQ=="; | ||
|
||
@implementation GACAppCheckTokenResult | ||
|
||
- (instancetype)initWithToken:(GACAppCheckToken *)token error:(NSError *)error { | ||
if (self = [super init]) { | ||
_token = token; | ||
_error = error; | ||
} | ||
|
||
return self; | ||
} | ||
|
||
- (instancetype)initWithToken:(GACAppCheckToken *)token { | ||
return [self initWithToken:token error:nil]; | ||
} | ||
|
||
- (instancetype)initWithError:(NSError *)error { | ||
return [self initWithToken:[[self class] placeholderToken] error:error]; | ||
} | ||
|
||
#pragma mark - Internal | ||
|
||
+ (GACAppCheckToken *)placeholderToken { | ||
return [[GACAppCheckToken alloc] initWithToken:kPlaceholderTokenValue | ||
expirationDate:[NSDate distantPast]]; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
AppCheckCore/Sources/Public/AppCheckCore/GACAppCheckTokenResult.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2023 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 <Foundation/Foundation.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@class GACAppCheckToken; | ||
|
||
NS_SWIFT_NAME(AppCheckCoreTokenResult) | ||
@interface GACAppCheckTokenResult : NSObject | ||
|
||
/// An App Check token in the case of success or a placeholder token in the case of a failure. | ||
@property(nonatomic, readonly) GACAppCheckToken *token; | ||
|
||
/// A token fetch error in the case of a failure or `nil` in the case of success. | ||
@property(nonatomic, readonly, nullable) NSError *error; | ||
|
||
- (instancetype)initWithToken:(GACAppCheckToken *)token; | ||
|
||
- (instancetype)initWithError:(NSError *)error; | ||
|
||
- (instancetype)initWithToken:(GACAppCheckToken *)token | ||
error:(nullable NSError *)error NS_DESIGNATED_INITIALIZER; | ||
|
||
- (instancetype)init NS_UNAVAILABLE; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
93 changes: 93 additions & 0 deletions
93
AppCheckCore/Tests/Unit/Core/GACAppCheckTokenResultTests.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Copyright 2023 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; | ||
|
||
#import "AppCheckCore/Sources/Public/AppCheckCore/GACAppCheckToken.h" | ||
#import "AppCheckCore/Sources/Public/AppCheckCore/GACAppCheckTokenResult.h" | ||
|
||
static NSString *const kTestTokenValue = @"test-token"; | ||
/// Placeholder value that indicates failure: `{"error":"UNKNOWN_ERROR"}` encoded as base64 | ||
static NSString *const kPlaceholderTokenValue = @"eyJlcnJvciI6IlVOS05PV05fRVJST1IifQ=="; | ||
static NSString *const kTestErrorDomain = @"TestErrorDomain"; | ||
static NSInteger const kTestErrorCode = 42; | ||
|
||
@interface GACAppCheckTokenResult (Tests) | ||
|
||
+ (GACAppCheckToken *)placeholderToken; | ||
|
||
@end | ||
|
||
@interface GACAppCheckTokenResultTests : XCTestCase | ||
@end | ||
|
||
@implementation GACAppCheckTokenResultTests | ||
|
||
- (void)testInitWithToken { | ||
NSDate *expectedExpirationDate = [NSDate dateWithTimeIntervalSince1970:1693314000.0]; | ||
NSDate *expectedReceivedAtDate = [NSDate dateWithTimeIntervalSince1970:1693317600.0]; | ||
GACAppCheckToken *expectedToken = [[GACAppCheckToken alloc] initWithToken:kTestTokenValue | ||
expirationDate:expectedExpirationDate | ||
receivedAtDate:expectedReceivedAtDate]; | ||
|
||
GACAppCheckTokenResult *tokenResult = | ||
[[GACAppCheckTokenResult alloc] initWithToken:expectedToken]; | ||
|
||
XCTAssertEqualObjects(tokenResult.token, expectedToken); | ||
XCTAssertNil(tokenResult.error); | ||
} | ||
|
||
- (void)testInitWithError { | ||
NSError *expectedError = [NSError errorWithDomain:kTestErrorDomain | ||
code:kTestErrorCode | ||
userInfo:nil]; | ||
|
||
GACAppCheckTokenResult *tokenResult = | ||
[[GACAppCheckTokenResult alloc] initWithError:expectedError]; | ||
|
||
XCTAssertEqualObjects(tokenResult.token.token, kPlaceholderTokenValue); | ||
XCTAssertNotNil(tokenResult.error); | ||
XCTAssertEqualObjects(tokenResult.error, expectedError); | ||
} | ||
|
||
- (void)testInitWithTokenAndError { | ||
GACAppCheckToken *placeholderToken = [GACAppCheckTokenResult placeholderToken]; | ||
NSError *expectedError = [NSError errorWithDomain:kTestErrorDomain | ||
code:kTestErrorCode | ||
userInfo:nil]; | ||
|
||
GACAppCheckTokenResult *tokenResult = | ||
[[GACAppCheckTokenResult alloc] initWithToken:placeholderToken error:expectedError]; | ||
|
||
XCTAssertEqualObjects(tokenResult.token, placeholderToken); | ||
XCTAssertNotNil(tokenResult.error); | ||
XCTAssertEqualObjects(tokenResult.error, expectedError); | ||
} | ||
|
||
- (void)testPlaceholderToken { | ||
NSDate *expectedExpirationDate = [NSDate distantPast]; | ||
NSDate *expectedReceivedAtDate = [NSDate date]; // Current time | ||
|
||
GACAppCheckToken *placeholderToken = [GACAppCheckTokenResult placeholderToken]; | ||
|
||
XCTAssertEqualObjects(placeholderToken.token, kPlaceholderTokenValue); | ||
// Verify that the placeholder token's received at time is approximately equal to current time. | ||
XCTAssertEqualWithAccuracy( | ||
[placeholderToken.receivedAtDate timeIntervalSinceDate:expectedReceivedAtDate], 0, 5.0); | ||
XCTAssertEqualObjects(placeholderToken.expirationDate, expectedExpirationDate); | ||
} | ||
|
||
@end |