Skip to content

Commit 6ee3073

Browse files
committed
Add unit test for GIDGoogleUser old encoding format
1 parent 7f4aab8 commit 6ee3073

File tree

7 files changed

+153
-36
lines changed

7 files changed

+153
-36
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#import <Foundation/Foundation.h>
18+
19+
@class OIDAuthState;
20+
21+
NS_ASSUME_NONNULL_BEGIN
22+
23+
// Internal class for GIDGoogleUser NSCoding backward compatibility.
24+
@interface GIDAuthentication : NSObject <NSSecureCoding>
25+
26+
@property(nonatomic) OIDAuthState* authState;
27+
28+
- (instancetype)initWithAuthState:(OIDAuthState *)authState;
29+
30+
@end
31+
32+
NS_ASSUME_NONNULL_END
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#import "GoogleSignIn/Sources/GIDAuthentication.h"
18+
19+
#ifdef SWIFT_PACKAGE
20+
@import AppAuth;
21+
#else
22+
#import <AppAuth/AppAuth.h>
23+
#endif
24+
25+
NS_ASSUME_NONNULL_BEGIN
26+
27+
static NSString *const kAuthStateKey = @"authState";
28+
29+
@implementation GIDAuthentication
30+
31+
- (instancetype)initWithAuthState:(OIDAuthState *)authState {
32+
self = [super init];
33+
if (self) {
34+
self.authState = authState;
35+
}
36+
return self;
37+
}
38+
39+
#pragma mark - NSSecureCoding
40+
41+
+ (BOOL)supportsSecureCoding {
42+
return YES;
43+
}
44+
45+
- (nullable instancetype)initWithCoder:(NSCoder *)decoder {
46+
self = [super init];
47+
if (self) {
48+
self.authState = [decoder decodeObjectOfClass:[OIDAuthState class] forKey:kAuthStateKey];
49+
}
50+
return self;
51+
}
52+
53+
- (void)encodeWithCoder:(NSCoder *)encoder {
54+
[encoder encodeObject:self.authState forKey:kAuthStateKey];
55+
}
56+
57+
@end
58+
59+
NS_ASSUME_NONNULL_END

GoogleSignIn/Sources/GIDGoogleUser.m

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDConfiguration.h"
2020

2121
#import "GoogleSignIn/Sources/GIDAppAuthFetcherAuthorizationWithEMMSupport.h"
22+
#import "GoogleSignIn/Sources/GIDAuthentication.h"
2223
#import "GoogleSignIn/Sources/GIDEMMSupport.h"
2324
#import "GoogleSignIn/Sources/GIDProfileData_Private.h"
2425
#import "GoogleSignIn/Sources/GIDSignInPreferences.h"
@@ -49,39 +50,6 @@
4950
// Minimal time interval before expiration for the access token or it needs to be refreshed.
5051
static NSTimeInterval const kMinimalTimeToExpire = 60.0;
5152

52-
#pragma mark - GIDAuthentication
53-
54-
// Internal class for GIDGoogleUser NSCoding backward compatibility.
55-
@interface GIDAuthentication : NSObject <NSSecureCoding>
56-
57-
@property(nonatomic) OIDAuthState* authState;
58-
59-
@end
60-
61-
@implementation GIDAuthentication
62-
63-
#pragma mark - NSSecureCoding
64-
65-
+ (BOOL)supportsSecureCoding {
66-
return YES;
67-
}
68-
69-
- (nullable instancetype)initWithCoder:(NSCoder *)decoder {
70-
self = [super init];
71-
if (self) {
72-
self.authState = [decoder decodeObjectOfClass:[OIDAuthState class] forKey:kAuthStateKey];
73-
}
74-
return self;
75-
}
76-
77-
- (void)encodeWithCoder:(NSCoder *)encoder {
78-
[encoder encodeObject:self.authState forKey:kAuthStateKey];
79-
}
80-
81-
@end
82-
83-
#pragma mark - GIDGoogleUser
84-
8553
@implementation GIDGoogleUser {
8654
GIDConfiguration *_cachedConfiguration;
8755

@@ -328,7 +296,6 @@ + (BOOL)supportsSecureCoding {
328296
return YES;
329297
}
330298

331-
332299
- (nullable instancetype)initWithCoder:(NSCoder *)decoder {
333300
self = [super init];
334301
if (self) {

GoogleSignIn/Sources/GIDGoogleUser_Private.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
#import <GTMAppAuth/GTMAppAuth.h>
2525
#endif
2626

27-
NS_ASSUME_NONNULL_BEGIN
28-
2927
@class OIDAuthState;
3028

29+
NS_ASSUME_NONNULL_BEGIN
30+
3131
/// A completion block that takes a `GIDGoogleUser` or an error if the attempt to refresh tokens was unsuccessful.
3232
typedef void (^GIDGoogleUserCompletion)(GIDGoogleUser *_Nullable user, NSError *_Nullable error);
3333

GoogleSignIn/Tests/Unit/GIDGoogleUser+Testing.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,12 @@
2323
- (NSUInteger)hash;
2424

2525
@end
26+
27+
// The old format GIDGoogleUser containing a GIDAuthentication.
28+
// Note: remove this class when GIDGoogleUser no longer support old encoding.
29+
@interface GIDGoogleUserOldFormat : GIDGoogleUser
30+
31+
- (instancetype)initOldGIDGoogleUserWithAuthState:(OIDAuthState *)authState
32+
profileData:(GIDProfileData *)profileData;
33+
34+
@end

GoogleSignIn/Tests/Unit/GIDGoogleUser+Testing.m

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,19 @@
1414

1515
#import "GoogleSignIn/Tests/Unit/GIDGoogleUser+Testing.h"
1616

17+
#import "GoogleSignIn/Sources/GIDGoogleUser_Private.h"
18+
19+
#import "GoogleSignIn/Sources/GIDAuthentication.h"
1720
#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDConfiguration.h"
1821
#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDToken.h"
1922

2023
#import "GoogleSignIn/Tests/Unit/GIDConfiguration+Testing.h"
2124
#import "GoogleSignIn/Tests/Unit/GIDProfileData+Testing.h"
2225

26+
// Key constants used for encode and decode.
27+
static NSString *const kProfileDataKey = @"profileData";
28+
static NSString *const kAuthentication = @"authentication";
29+
2330
@implementation GIDGoogleUser (Testing)
2431

2532
- (BOOL)isEqual:(id)object {
@@ -48,3 +55,27 @@ - (NSUInteger)hash {
4855
}
4956

5057
@end
58+
59+
@implementation GIDGoogleUserOldFormat {
60+
GIDAuthentication *_authentication;
61+
GIDProfileData *_profile;
62+
}
63+
64+
- (instancetype)initOldGIDGoogleUserWithAuthState:(OIDAuthState *)authState
65+
profileData:(GIDProfileData *)profileData {
66+
self = [super initWithAuthState:authState profileData:profileData];
67+
if (self) {
68+
_authentication = [[GIDAuthentication alloc] initWithAuthState:authState];
69+
_profile = profileData;
70+
}
71+
return self;
72+
}
73+
74+
#pragma mark - NSSecureCoding
75+
76+
- (void)encodeWithCoder:(NSCoder *)encoder {
77+
[encoder encodeObject:_profile forKey:kProfileDataKey];
78+
[encoder encodeObject:_authentication forKey:kAuthentication];
79+
}
80+
81+
@end

GoogleSignIn/Tests/Unit/GIDGoogleUserTest.m

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDToken.h"
2222

2323
#import "GoogleSignIn/Sources/GIDGoogleUser_Private.h"
24+
#import "GoogleSignIn/Tests/Unit/GIDGoogleUser+Testing.h"
2425
#import "GoogleSignIn/Tests/Unit/GIDProfileData+Testing.h"
2526
#import "GoogleSignIn/Tests/Unit/OIDAuthState+Testing.h"
2627
#import "GoogleSignIn/Tests/Unit/OIDAuthorizationRequest+Testing.h"
@@ -132,6 +133,24 @@ - (void)testLegacyCoding {
132133
}
133134
#endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST
134135

136+
// Test the old encoding format for backword compatability.
137+
- (void)testOldFormatCoding {
138+
if (@available(iOS 11, macOS 10.13, *)) {
139+
OIDAuthState *authState = [OIDAuthState testInstance];
140+
GIDProfileData *profileDate = [GIDProfileData testInstance];
141+
GIDGoogleUserOldFormat *user =
142+
[[GIDGoogleUserOldFormat alloc] initOldGIDGoogleUserWithAuthState:authState
143+
profileData:profileDate];
144+
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:user
145+
requiringSecureCoding:YES
146+
error:nil];
147+
GIDGoogleUser *newUser = [NSKeyedUnarchiver unarchivedObjectOfClass:[GIDGoogleUser class]
148+
fromData:data
149+
error:nil];
150+
XCTAssertEqualObjects(user, newUser);
151+
}
152+
}
153+
135154
- (void)testUpdateAuthState {
136155
GIDGoogleUser *user = [self googleUserWithAccessTokenExpiresIn:kAccessTokenExpiresIn
137156
idTokenExpiresIn:kIDTokenExpiresIn];

0 commit comments

Comments
 (0)