-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathGIDAuthStateMigrationTest.m
More file actions
302 lines (250 loc) · 13.1 KB
/
GIDAuthStateMigrationTest.m
File metadata and controls
302 lines (250 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Copyright 2021 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>
#import "GoogleSignIn/Sources/GIDAuthStateMigration/GIDAuthStateMigration.h"
#import "GoogleSignIn/Sources/GIDSignInCallbackSchemes.h"
#if TARGET_OS_OSX
#import "GoogleSignIn/Tests/Unit/OIDAuthState+Testing.h"
#endif // TARGET_OS_OSX
@import GTMAppAuth;
#ifdef SWIFT_PACKAGE
@import AppAuth;
@import OCMock;
#else
#import <AppAuth/AppAuth.h>
#import <OCMock/OCMock.h>
#endif
static NSString *const kTokenURL = @"https://host.com/example/token/url";
static NSString *const kCallbackPath = @"/callback/path";
static NSString *const kKeychainName = @"keychain_name";
static NSString *const kBundleID = @"com.google.GoogleSignInInternalSample.dev";
static NSString *const kClientID =
@"223520599684-kg64hfn0h950oureqacja2fltg00msv3.apps.googleusercontent.com";
static NSString *const kDotReversedClientID =
@"com.googleusercontent.apps.223520599684-kg64hfn0h950oureqacja2fltg00msv3";
static NSString *const kSavedFingerprint = @"com.google.GoogleSignInInternalSample.dev-"
"223520599684-kg64hfn0h950oureqacja2fltg00msv3.apps.googleusercontent.com-email profile";
static NSString *const kSavedFingerprint_HostedDomain =
@"com.google.GoogleSignInInternalSample.dev-"
"223520599684-kg64hfn0h950oureqacja2fltg00msv3.apps.googleusercontent.com-email profile-"
"hd=test.com";
static NSString *const kGTMOAuth2PersistenceString = @"param1=value1¶m2=value2";
static NSString *const kAdditionalTokenRequestParametersPostfix = @"~~atrp";
static NSString *const kAdditionalTokenRequestParameters = @"param3=value3¶m4=value4";
static NSString *const kFinalPersistenceString =
@"param1=value1¶m2=value2¶m3=value3¶m4=value4";
static NSString *const kRedirectURI =
@"com.googleusercontent.apps.223520599684-kg64hfn0h950oureqacja2fltg00msv3:/callback/path";
static NSString *const kGTMAppAuthMigrationCheckPerformedKey = @"GID_MigrationCheckPerformed";
static NSString *const kDataProtectedMigrationCheckPerformedKey =
@"GID_DataProtectedMigrationCheckPerformed";
static NSString *const kFingerprintService = @"fingerprint";
NS_ASSUME_NONNULL_BEGIN
@interface GIDAuthStateMigration ()
+ (nullable NSString *)passwordForService:(NSString *)service;
/// Returns a `GTMAuthSession` given the provided token URL.
///
/// This method enables using an instance of `GIDAuthStateMigration` that is created with a fake
/// `GTMKeychainStore` and thereby minimizes mocking.
- (nullable GTMAuthSession *)
extractAuthSessionWithTokenURL:(NSURL *)tokenURL callbackPath:(NSString *)callbackPath;
@end
@interface GIDAuthStateMigrationTest : XCTestCase
@end
@implementation GIDAuthStateMigrationTest {
id _mockUserDefaults;
id _mockGTMAppAuthFetcherAuthorization;
id _mockGIDAuthStateMigration;
id _mockGTMKeychainStore;
id _mockKeychainHelper;
id _mockNSBundle;
id _mockGIDSignInCallbackSchemes;
id _mockGTMOAuth2Compatibility;
#if TARGET_OS_OSX
id _realLegacyGTMKeychainStore;
#endif // TARGET_OS_OSX
}
- (void)setUp {
[super setUp];
_mockUserDefaults = OCMClassMock([NSUserDefaults class]);
_mockGTMAppAuthFetcherAuthorization = OCMStrictClassMock([GTMAuthSession class]);
_mockGIDAuthStateMigration = OCMStrictClassMock([GIDAuthStateMigration class]);
_mockGTMKeychainStore = OCMStrictClassMock([GTMKeychainStore class]);
_mockKeychainHelper = OCMProtocolMock(@protocol(GTMKeychainHelper));
_mockNSBundle = OCMStrictClassMock([NSBundle class]);
_mockGIDSignInCallbackSchemes = OCMStrictClassMock([GIDSignInCallbackSchemes class]);
_mockGTMOAuth2Compatibility = OCMStrictClassMock([GTMOAuth2Compatibility class]);
#if TARGET_OS_OSX
GTMKeychainAttribute *fileBasedKeychain = [GTMKeychainAttribute useFileBasedKeychain];
NSSet *attributes = [NSSet setWithArray:@[fileBasedKeychain]];
_realLegacyGTMKeychainStore = [[GTMKeychainStore alloc] initWithItemName:kKeychainName
keychainAttributes:attributes];
#endif // TARGET_OS_OSX
}
- (void)tearDown {
[_mockUserDefaults verify];
[_mockUserDefaults stopMocking];
[_mockGTMAppAuthFetcherAuthorization verify];
[_mockGTMAppAuthFetcherAuthorization stopMocking];
[_mockGIDAuthStateMigration verify];
[_mockGIDAuthStateMigration stopMocking];
[_mockGTMKeychainStore verify];
[_mockGTMKeychainStore stopMocking];
[_mockKeychainHelper verify];
[_mockKeychainHelper stopMocking];
[_mockNSBundle verify];
[_mockNSBundle stopMocking];
[_mockGIDSignInCallbackSchemes verify];
[_mockGIDSignInCallbackSchemes stopMocking];
[_mockGTMOAuth2Compatibility verify];
[_mockGTMOAuth2Compatibility stopMocking];
#if TARGET_OS_OSX
[_realLegacyGTMKeychainStore removeAuthSessionWithError:nil];
#endif // TARGET_OS_OSX
[super tearDown];
}
#pragma mark - Tests
#if TARGET_OS_OSX
- (void)testMigrateIfNeeded_NoPreviousMigration_DataProtectedMigration {
[[[_mockUserDefaults stub] andReturn:_mockUserDefaults] standardUserDefaults];
[[[_mockUserDefaults expect] andReturnValue:@NO] boolForKey:kDataProtectedMigrationCheckPerformedKey];
[[_mockUserDefaults expect] setBool:YES forKey:kDataProtectedMigrationCheckPerformedKey];
[[_mockGTMKeychainStore expect] saveAuthSession:OCMOCK_ANY error:OCMArg.anyObjectRef];
[[[_mockGTMKeychainStore expect] andReturn:kKeychainName] itemName];
// set the auth session that will be migrated
OIDAuthState *authState = [OIDAuthState testInstance];
GTMAuthSession *authSession = [[GTMAuthSession alloc] initWithAuthState:authState];
NSError *err;
[_realLegacyGTMKeychainStore saveAuthSession:authSession error:&err];
XCTAssertNil(err);
GIDAuthStateMigration *migration =
[[GIDAuthStateMigration alloc] initWithKeychainStore:_mockGTMKeychainStore];
[migration migrateIfNeededWithTokenURL:[NSURL URLWithString:kTokenURL]
callbackPath:kCallbackPath
isFreshInstall:NO];
// verify that the auth session was removed during migration
XCTAssertNil([_realLegacyGTMKeychainStore retrieveAuthSessionWithError:nil]);
}
- (void)testMigrateIfNeeded_KeychainFailure_DataProtectedMigration {
[[[_mockUserDefaults stub] andReturn:_mockUserDefaults] standardUserDefaults];
[[[_mockUserDefaults expect] andReturnValue:@NO] boolForKey:kDataProtectedMigrationCheckPerformedKey];
NSError *keychainSaveError = [NSError new];
OCMStub([_mockGTMKeychainStore saveAuthSession:OCMOCK_ANY error:[OCMArg setTo:keychainSaveError]]);
[[[_mockGTMKeychainStore expect] andReturn:kKeychainName] itemName];
OIDAuthState *authState = [OIDAuthState testInstance];
GTMAuthSession *authSession = [[GTMAuthSession alloc] initWithAuthState:authState];
NSError *err;
[_realLegacyGTMKeychainStore saveAuthSession:authSession error:&err];
XCTAssertNil(err);
GIDAuthStateMigration *migration =
[[GIDAuthStateMigration alloc] initWithKeychainStore:_mockGTMKeychainStore];
[migration migrateIfNeededWithTokenURL:[NSURL URLWithString:kTokenURL]
callbackPath:kCallbackPath
isFreshInstall:NO];
XCTAssertNil([_realLegacyGTMKeychainStore retrieveAuthSessionWithError:nil]);
}
#elif TARGET_OS_IOS && !TARGET_OS_MACCATALYST
- (void)testMigrateIfNeeded_NoPreviousMigration_GTMAppAuthMigration {
[[[_mockUserDefaults stub] andReturn:_mockUserDefaults] standardUserDefaults];
[[[_mockUserDefaults expect] andReturnValue:@NO] boolForKey:kGTMAppAuthMigrationCheckPerformedKey];
[[_mockUserDefaults expect] setBool:YES forKey:kGTMAppAuthMigrationCheckPerformedKey];
[[_mockGTMKeychainStore expect] saveAuthSession:OCMOCK_ANY error:OCMArg.anyObjectRef];
[self setUpCommonExtractAuthorizationMocksWithFingerPrint:kSavedFingerprint];
GIDAuthStateMigration *migration =
[[GIDAuthStateMigration alloc] initWithKeychainStore:_mockGTMKeychainStore];
[migration migrateIfNeededWithTokenURL:[NSURL URLWithString:kTokenURL]
callbackPath:kCallbackPath
isFreshInstall:NO];
}
- (void)testMigrateIfNeeded_KeychainFailure_GTMAppAuthMigration {
[[[_mockUserDefaults stub] andReturn:_mockUserDefaults] standardUserDefaults];
[[[_mockUserDefaults expect] andReturnValue:@NO] boolForKey:kGTMAppAuthMigrationCheckPerformedKey];
NSError *keychainSaveError = [NSError new];
OCMStub([_mockGTMKeychainStore saveAuthSession:OCMOCK_ANY error:[OCMArg setTo:keychainSaveError]]);
[self setUpCommonExtractAuthorizationMocksWithFingerPrint:kSavedFingerprint];
GIDAuthStateMigration *migration =
[[GIDAuthStateMigration alloc] initWithKeychainStore:_mockGTMKeychainStore];
[migration migrateIfNeededWithTokenURL:[NSURL URLWithString:kTokenURL]
callbackPath:kCallbackPath
isFreshInstall:NO];
}
- (void)testExtractAuthorization {
[self setUpCommonExtractAuthorizationMocksWithFingerPrint:kSavedFingerprint];
GIDAuthStateMigration *migration =
[[GIDAuthStateMigration alloc] initWithKeychainStore:_mockGTMKeychainStore];
GTMAuthSession *authorization =
[migration extractAuthSessionWithTokenURL:[NSURL URLWithString:kTokenURL]
callbackPath:kCallbackPath];
XCTAssertNotNil(authorization);
}
- (void)testExtractAuthorization_HostedDomain {
[self setUpCommonExtractAuthorizationMocksWithFingerPrint:kSavedFingerprint_HostedDomain];
GIDAuthStateMigration *migration =
[[GIDAuthStateMigration alloc] initWithKeychainStore:_mockGTMKeychainStore];
GTMAuthSession *authorization =
[migration extractAuthSessionWithTokenURL:[NSURL URLWithString:kTokenURL]
callbackPath:kCallbackPath];
XCTAssertNotNil(authorization);
}
#endif // TARGET_OS_OSX
- (void)testMigrateIfNeeded_HasPreviousMigration {
[[[_mockUserDefaults stub] andReturn:_mockUserDefaults] standardUserDefaults];
#if TARGET_OS_OSX
[[[_mockUserDefaults expect] andReturnValue:@YES] boolForKey:kDataProtectedMigrationCheckPerformedKey];
[[_mockUserDefaults reject] setBool:YES forKey:kDataProtectedMigrationCheckPerformedKey];
#elif TARGET_OS_IOS && !TARGET_OS_MACCATALYST
[[[_mockUserDefaults expect] andReturnValue:@YES] boolForKey:kGTMAppAuthMigrationCheckPerformedKey];
[[_mockUserDefaults reject] setBool:YES forKey:kGTMAppAuthMigrationCheckPerformedKey];
#endif // TARGET_OS_OSX
GIDAuthStateMigration *migration =
[[GIDAuthStateMigration alloc] initWithKeychainStore:_mockGTMKeychainStore];
[migration migrateIfNeededWithTokenURL:[NSURL URLWithString:kTokenURL]
callbackPath:kCallbackPath
isFreshInstall:NO];
}
- (void)testMigrateIfNeeded_isFreshInstall {
[[[_mockUserDefaults stub] andReturn:_mockUserDefaults] standardUserDefaults];
#if TARGET_OS_OSX
[[_mockUserDefaults expect] setBool:YES forKey:kDataProtectedMigrationCheckPerformedKey];
#elif TARGET_OS_IOS && !TARGET_OS_MACCATALYST
[[_mockUserDefaults expect] setBool:YES forKey:kGTMAppAuthMigrationCheckPerformedKey];
#endif // TARGET_OS_OSX
GIDAuthStateMigration *migration =
[[GIDAuthStateMigration alloc] initWithKeychainStore:_mockGTMKeychainStore];
[migration migrateIfNeededWithTokenURL:[NSURL URLWithString:kTokenURL]
callbackPath:kCallbackPath
isFreshInstall:YES];
}
#pragma mark - Helpers
// Generate the service name for the stored additional token request parameters string.
- (NSString *)additionalTokenRequestParametersKeyFromFingerprint:(NSString *)fingerprint {
return [NSString stringWithFormat:@"%@%@", fingerprint, kAdditionalTokenRequestParametersPostfix];
}
- (void)setUpCommonExtractAuthorizationMocksWithFingerPrint:(NSString *)fingerprint {
[[[_mockGIDAuthStateMigration expect] andReturn:fingerprint]
passwordForService:kFingerprintService];
(void)[[[_mockKeychainHelper expect] andReturn:kGTMOAuth2PersistenceString]
passwordForService:fingerprint error:OCMArg.anyObjectRef];
[[[_mockGTMKeychainStore expect] andReturn:_mockKeychainHelper] keychainHelper];
[[[_mockNSBundle expect] andReturn:_mockNSBundle] mainBundle];
[[[_mockNSBundle expect] andReturn:kBundleID] bundleIdentifier];
[[[_mockGIDSignInCallbackSchemes expect] andReturn:_mockGIDSignInCallbackSchemes] alloc];
(void)[[[_mockGIDSignInCallbackSchemes expect] andReturn:_mockGIDSignInCallbackSchemes]
initWithClientIdentifier:kClientID];
[[[_mockGIDSignInCallbackSchemes expect] andReturn:kDotReversedClientID] clientIdentifierScheme];
[[[_mockGIDAuthStateMigration expect] andReturn:kAdditionalTokenRequestParameters]
passwordForService:[self additionalTokenRequestParametersKeyFromFingerprint:fingerprint]];
}
@end
NS_ASSUME_NONNULL_END