forked from openid/AppAuth-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OIDServiceConfigurationTests.m
386 lines (331 loc) · 15.8 KB
/
OIDServiceConfigurationTests.m
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*! @file OIDServiceConfigurationTests.m
@brief AppAuth iOS SDK
@copyright
Copyright 2015 Google Inc. All Rights Reserved.
@copydetails
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 "OIDServiceConfigurationTests.h"
#import <objc/runtime.h>
#import "OIDServiceDiscoveryTests.h"
#import "Source/OIDAuthorizationService.h"
#import "Source/OIDError.h"
#import "Source/OIDServiceConfiguration.h"
#import "Source/OIDServiceDiscovery.h"
/*! @typedef DataTaskWithURLCompletionHandler
@brief The callback signature for @c NSURLSession 's @c dataTaskWithURL:completionHandler:
method, which we swizzle in @c testFetcher to fake the network response with an OpenID
Connect Discovery document.
*/
typedef void(^DataTaskWithURLCompletionHandler)(NSData *_Nullable data,
NSURLResponse *_Nullable response,
NSError *_Nullable error);
/*! @typedef DataTaskWithURLCompletionImplementation
@brief The function signature for a @c dataTaskWithURL:completionHandler: implementation. Used
in @c testFetcher for implementing a swizzled version of @c NSURLSession 's
@c dataTaskWithURL:completionHandler:
*/
typedef NSURLSessionDataTask *(^DataTaskWithURLCompletionImplementation)
(id _self, NSURL *url, DataTaskWithURLCompletionHandler completionHandler);
/*! @typedef TeardownTask
@brief A block to be called during teardown.
*/
typedef void(^TeardownTask)();
/*! @var kInitializerTestAuthEndpoint
@brief Test value for the @c authorizationEndpoint property.
*/
static NSString *const kInitializerTestAuthEndpoint = @"https://www.example.com/auth";
/*! @var kInitializerTestTokenEndpoint
@brief Test value for the @c tokenEndpoint property.
*/
static NSString *const kInitializerTestTokenEndpoint = @"https://www.example.com/token";
/*! @var kInitializerTestDiscoveryEndpoint
@brief Test URL for OpenID Connect Discovery document. Not actually retrieved.
*/
static NSString *const kInitializerTestDiscoveryEndpoint = @"https://www.example.com/discovery";
/*! @var kIssuerTestIssuer
@brief Test issuer for OpenID Connect discovery
*/
static NSString *const kIssuerTestIssuer = @"https://accounts.google.com/";
/*! @var kIssuerTestIssuer2
@brief Test issuer without a slash for OpenID Connect discovery
*/
static NSString *const kIssuerTestIssuer2 = @"https://accounts.google.com";
/*! @var kIssuerTestExpectedFullDiscoveryURL
@brief Test complete valid discovery URL
*/
static NSString *const kIssuerTestExpectedFullDiscoveryURL =
@"https://accounts.google.com/.well-known/openid-configuration";
@implementation OIDServiceConfigurationTests {
/*! @var _teardownTasks
@brief A list of tasks to perform during tearDown.
*/
NSMutableArray<TeardownTask> *_teardownTasks;
}
+ (OIDServiceConfiguration *)testInstance {
NSURL *authEndpoint = [NSURL URLWithString:kInitializerTestAuthEndpoint];
NSURL *tokenEndpoint = [NSURL URLWithString:kInitializerTestTokenEndpoint];
OIDServiceConfiguration *configuration =
[[OIDServiceConfiguration alloc] initWithAuthorizationEndpoint:authEndpoint
tokenEndpoint:tokenEndpoint];
return configuration;
}
- (void)setUp {
_teardownTasks = [NSMutableArray array];
}
- (void)tearDown {
for (TeardownTask task in _teardownTasks) {
task();
}
_teardownTasks = nil;
}
/*! @fn replaceMethod:withBlock:
@brief Replaces the given method with a block for testing, undoing the change during tearDown.
@param method The method to replace.
@param block The new implementation of the method to be used.
*/
- (void)replaceMethod:(Method)method withBlock:(id)block {
IMP originalImpl = method_getImplementation(method);
IMP testImpl = imp_implementationWithBlock(block);
// swizzles the method
method_setImplementation(method, testImpl);
// unswizzles the method during teardown
[_teardownTasks addObject:^(){
method_setImplementation(method, originalImpl);
}];
}
/*! @fn replaceInstanceMethodForClass:selector:withBlock:
@brief Replaces the given instance method with a block for testing, reversing the change during
tearDown.
@param class The class whose method will be replaced.
@param selector The selector of the class method that will be replaced.
@param block The new implementation of the method to be used.
*/
- (void)replaceInstanceMethodForClass:(Class)class selector:(SEL)selector withBlock:(id)block {
Method method = class_getInstanceMethod(class, selector);
[self replaceMethod:method withBlock:block];
}
/*! @fn replaceClassMethodForClass:selector:withBlock:
@brief Replaces the given class method with a block for testing, reversing the change during
tearDown.
@param class The class whose method will be replaced.
@param selector The selector of the class method that will be replaced.
@param block The new implementation of the method to be used.
*/
- (void)replaceClassMethodForClass:(Class)class selector:(SEL)selector withBlock:(id)block {
Method method = class_getClassMethod(class, selector);
[self replaceMethod:method withBlock:block];
}
/*! @fn testInitializer
@brief Tests the designated initializer.
*/
- (void)testInitializer {
OIDServiceConfiguration *configuration = [[self class] testInstance];
XCTAssertEqualObjects(configuration.authorizationEndpoint.absoluteString,
kInitializerTestAuthEndpoint);
XCTAssertEqualObjects(configuration.tokenEndpoint.absoluteString,
kInitializerTestTokenEndpoint);
}
- (void)testIssuer {
[self discoveryWithIssuer:kIssuerTestIssuer];
}
- (void)testIssuer2 {
[self discoveryWithIssuer:kIssuerTestIssuer2];
}
- (void)discoveryWithIssuer:(NSString *)issuer {
XCTestExpectation *expectation =
[self expectationWithDescription:@"Discovery URL should be correct."];
id successfulResponse =
^(id _self, NSURL *discoveryURL, OIDDiscoveryCallback completion) {
NSURL *fullDiscoveryURL = [NSURL URLWithString:kIssuerTestExpectedFullDiscoveryURL];
if ([discoveryURL isEqual:fullDiscoveryURL]) {
[expectation fulfill];
return;
}
XCTAssert(NO,
@"Not equal %@ != %@",
[fullDiscoveryURL absoluteString],
[discoveryURL absoluteString]);
};
[self replaceClassMethodForClass:[OIDAuthorizationService class]
selector:@selector(discoverServiceConfigurationForDiscoveryURL:completion:)
withBlock:successfulResponse];
NSURL *issuerURL = [NSURL URLWithString:issuer];
[OIDAuthorizationService discoverServiceConfigurationForIssuer:issuerURL
completion:^(OIDServiceConfiguration *_Nullable configuration, NSError *_Nullable error) {}];
[self waitForExpectationsWithTimeout:2 handler:nil];
}
/*! @fn testFetcher
@brief Tests the OpenID Connect Discovery Document fetching and initialization.
*/
- (void)testFetcher {
DataTaskWithURLCompletionImplementation successfulResponse =
^NSURLSessionDataTask *(
id _self, NSURL *url, DataTaskWithURLCompletionHandler completionHandler) {
NSError *error;
NSDictionary *jsonObject =
[OIDServiceDiscoveryTests completeServiceDiscoveryDictionary];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonObject
options:NSJSONWritingPrettyPrinted
error:&error];
NSHTTPURLResponse *jsonResponse =
[[NSHTTPURLResponse alloc] initWithURL:url
statusCode:200
HTTPVersion:@"1.1"
headerFields:nil];
completionHandler(jsonData, jsonResponse, nil);
return nil;
};
[self replaceInstanceMethodForClass:[NSURLSession class]
selector:@selector(dataTaskWithURL:completionHandler:)
withBlock:successfulResponse];
NSURL *url = [NSURL URLWithString:kInitializerTestDiscoveryEndpoint];
NSDictionary *expectedDictionary =
[OIDServiceDiscoveryTests completeServiceDiscoveryDictionary];
OIDServiceDiscovery *expectedValues =
[[OIDServiceDiscovery alloc] initWithDictionary:expectedDictionary error:NULL];
XCTestExpectation *expectation = [self expectationWithDescription:@"Callback should be fired."];
OIDServiceConfigurationCreated callback =
^(OIDServiceConfiguration *_Nullable serviceConfiguration,
NSError *_Nullable error) {
[expectation fulfill];
XCTAssertNil(error);
XCTAssertNotNil(serviceConfiguration);
XCTAssertEqualObjects(serviceConfiguration.tokenEndpoint,
expectedValues.tokenEndpoint);
XCTAssertEqualObjects(serviceConfiguration.authorizationEndpoint,
expectedValues.authorizationEndpoint);
};
[OIDAuthorizationService discoverServiceConfigurationForDiscoveryURL:url
completion:callback];
[self waitForExpectationsWithTimeout:2 handler:nil];
}
/*! @fn testFetcherWithNetworkError
@brief Tests the OpenID Connect Discovery Document fetching and initialization in the face of
a network error.
*/
- (void)testFetcherWithNetworkError {
DataTaskWithURLCompletionImplementation successfulResponse =
^NSURLSessionDataTask *(
id _self, NSURL *url, DataTaskWithURLCompletionHandler completionHandler) {
NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:500 userInfo:nil];
completionHandler(nil, nil, error);
return nil;
};
[self replaceInstanceMethodForClass:[NSURLSession class]
selector:@selector(dataTaskWithURL:completionHandler:)
withBlock:successfulResponse];
NSURL *url = [NSURL URLWithString:kInitializerTestDiscoveryEndpoint];
XCTestExpectation *expectation = [self expectationWithDescription:@"Callback should be fired."];
OIDServiceConfigurationCreated callback =
^(OIDServiceConfiguration *_Nullable serviceConfiguration,
NSError *_Nullable error) {
[expectation fulfill];
XCTAssertNotNil(error);
XCTAssertNil(serviceConfiguration);
};
[OIDAuthorizationService discoverServiceConfigurationForDiscoveryURL:url
completion:callback];
[self waitForExpectationsWithTimeout:2 handler:nil];
}
/*! @fn testFetcherWithErrorCode
@brief Tests the OpenID Connect Discovery Document fetching and initialization in the face of
a non-2xx HTTP status code. Should return an error.
*/
- (void)testFetcherWithErrorCode {
DataTaskWithURLCompletionImplementation successfulResponse =
^NSURLSessionDataTask *(
id _self, NSURL *url, DataTaskWithURLCompletionHandler completionHandler) {
NSError *error;
NSDictionary *jsonObject = [OIDServiceDiscoveryTests completeServiceDiscoveryDictionary];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonObject
options:NSJSONWritingPrettyPrinted
error:&error];
NSHTTPURLResponse *jsonResponse =
[[NSHTTPURLResponse alloc] initWithURL:url
statusCode:500
HTTPVersion:@"1.1"
headerFields:nil];
completionHandler(jsonData, jsonResponse, nil);
return nil;
};
[self replaceInstanceMethodForClass:[NSURLSession class]
selector:@selector(dataTaskWithURL:completionHandler:)
withBlock:successfulResponse];
NSURL *url = [NSURL URLWithString:kInitializerTestDiscoveryEndpoint];
XCTestExpectation *expectation = [self expectationWithDescription:@"Callback should be fired."];
OIDServiceConfigurationCreated callback =
^(OIDServiceConfiguration *_Nullable serviceConfiguration,
NSError *_Nullable error) {
[expectation fulfill];
XCTAssertNotNil(error);
XCTAssertNil(serviceConfiguration);
};
[OIDAuthorizationService discoverServiceConfigurationForDiscoveryURL:url
completion:callback];
[self waitForExpectationsWithTimeout:2 handler:nil];
}
/*! @fn testFetcherWithBadJSON
@brief Tests the OpenID Connect Discovery Document fetching and initialization in the face of
bad JSON input.
*/
- (void)testFetcherWithBadJSON {
DataTaskWithURLCompletionImplementation successfulResponse =
^NSURLSessionDataTask *(
id _self, NSURL *url, DataTaskWithURLCompletionHandler completionHandler) {
NSData *jsonData = [@"JUNK" dataUsingEncoding:NSUTF8StringEncoding];
NSHTTPURLResponse *jsonResponse =
[[NSHTTPURLResponse alloc] initWithURL:url
statusCode:200
HTTPVersion:@"1.1"
headerFields:nil];
completionHandler(jsonData, jsonResponse, nil);
return nil;
};
[self replaceInstanceMethodForClass:[NSURLSession class]
selector:@selector(dataTaskWithURL:completionHandler:)
withBlock:successfulResponse];
NSURL *url = [NSURL URLWithString:kInitializerTestDiscoveryEndpoint];
XCTestExpectation *expectation = [self expectationWithDescription:@"Callback should be fired."];
OIDServiceConfigurationCreated callback =
^(OIDServiceConfiguration *_Nullable serviceConfiguration,
NSError *_Nullable error) {
[expectation fulfill];
XCTAssertNotNil(error);
XCTAssertNil(serviceConfiguration);
};
[OIDAuthorizationService discoverServiceConfigurationForDiscoveryURL:url
completion:callback];
[self waitForExpectationsWithTimeout:2 handler:nil];
}
/*! @fn testSecureCoding
@brief Tests the @c NSSecureCoding by round-tripping an instance through the coding process and
checking to make sure the source and destination instances have equivalent dictionaries.
*/
- (void)testSecureCoding {
OIDServiceConfiguration *configuration = [[self class] testInstance];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:configuration];
OIDServiceConfiguration *unarchived = [NSKeyedUnarchiver unarchiveObjectWithData:data];
XCTAssertEqualObjects(configuration.authorizationEndpoint, unarchived.authorizationEndpoint);
XCTAssertEqualObjects(configuration.tokenEndpoint, unarchived.tokenEndpoint);
}
/*! @fn testCopying
@brief Tests the @c NSCopying implementation by round-tripping an instance through the copying
process and checking to make sure the source and destination instances have equivalent
dictionaries.
*/
- (void)testCopying {
OIDServiceConfiguration *configuration = [[self class] testInstance];
OIDServiceConfiguration *unarchived = [configuration copy];
XCTAssertEqualObjects(configuration.authorizationEndpoint, unarchived.authorizationEndpoint);
XCTAssertEqualObjects(configuration.tokenEndpoint, unarchived.tokenEndpoint);
}
@end