@@ -279,6 +279,89 @@ class DecodeTests: XCTestCase {
279
279
}
280
280
}
281
281
282
+ class ValidationTests : XCTestCase {
283
+ func testClaimJustExpiredWithoutLeeway( ) {
284
+ var claims = ClaimSet ( )
285
+ claims. expiration = Date ( ) . addingTimeInterval ( - 1 )
286
+
287
+ let expectation = XCTestExpectation ( description: " Signature should be expired. " )
288
+ do {
289
+ try claims. validateExpiary ( )
290
+ XCTFail ( " InvalidToken.expiredSignature error should have been thrown. " )
291
+ } catch InvalidToken . expiredSignature {
292
+ expectation. fulfill ( )
293
+ } catch {
294
+ XCTFail ( " Unexpected error while validating exp claim. " )
295
+ }
296
+ self . wait ( for: [ expectation] , timeout: 0.5 )
297
+ }
298
+
299
+ func testClaimJustNotExpiredWithoutLeeway( ) {
300
+ var claims = ClaimSet ( )
301
+ claims. expiration = Date ( ) . addingTimeInterval ( - 1 )
302
+
303
+ do {
304
+ try claims. validateExpiary ( leeway: 2 )
305
+ } catch {
306
+ XCTFail ( " Unexpected error while validating exp claim that should be valid with leeway. " )
307
+ }
308
+ }
309
+
310
+ func testNotBeforeIsImmatureSignatureWithoutLeeway( ) {
311
+ var claims = ClaimSet ( )
312
+ claims. notBefore = Date ( ) . addingTimeInterval ( 1 )
313
+
314
+ let expectation = XCTestExpectation ( description: " Signature should be immature. " )
315
+ do {
316
+ try claims. validateNotBefore ( )
317
+ XCTFail ( " InvalidToken.immatureSignature error should have been thrown. " )
318
+ } catch InvalidToken . immatureSignature {
319
+ expectation. fulfill ( )
320
+ } catch {
321
+ XCTFail ( " Unexpected error while validating nbf claim. " )
322
+ }
323
+ self . wait ( for: [ expectation] , timeout: 0.5 )
324
+ }
325
+
326
+ func testNotBeforeIsValidWithLeeway( ) {
327
+ var claims = ClaimSet ( )
328
+ claims. notBefore = Date ( ) . addingTimeInterval ( 1 )
329
+
330
+ do {
331
+ try claims. validateNotBefore ( leeway: 2 )
332
+ } catch {
333
+ XCTFail ( " Unexpected error while validating nbf claim that should be valid with leeway. " )
334
+ }
335
+ }
336
+
337
+ func testIssuedAtIsInFutureWithoutLeeway( ) {
338
+ var claims = ClaimSet ( )
339
+ claims. issuedAt = Date ( ) . addingTimeInterval ( 1 )
340
+
341
+ let expectation = XCTestExpectation ( description: " iat should be in the future. " )
342
+ do {
343
+ try claims. validateIssuedAt ( )
344
+ XCTFail ( " InvalidToken.invalidIssuedAt error should have been thrown. " )
345
+ } catch InvalidToken . invalidIssuedAt {
346
+ expectation. fulfill ( )
347
+ } catch {
348
+ XCTFail ( " Unexpected error while validating iat claim. " )
349
+ }
350
+ self . wait ( for: [ expectation] , timeout: 0.5 )
351
+ }
352
+
353
+ func testIssuedAtIsValidWithLeeway( ) {
354
+ var claims = ClaimSet ( )
355
+ claims. issuedAt = Date ( ) . addingTimeInterval ( 1 )
356
+
357
+ do {
358
+ try claims. validateIssuedAt ( leeway: 2 )
359
+ } catch {
360
+ XCTFail ( " Unexpected error while validating iat claim that should be valid with leeway. " )
361
+ }
362
+ }
363
+ }
364
+
282
365
// MARK: Helpers
283
366
284
367
func assertSuccess( _ decoder: @autoclosure ( ) throws -> Payload , closure: ( ( Payload ) -> Void ) ? = nil ) {
0 commit comments