summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.Test/Messaging/Bindings/StandardExpirationBindingElementTests.cs
blob: a8eb7c1071a709ede18d67f9e0580fadfc920f8c (plain)
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
//-----------------------------------------------------------------------
// <copyright file="StandardExpirationBindingElementTests.cs" company="Outercurve Foundation">
//     Copyright (c) Outercurve Foundation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.Test.Messaging.Bindings {
	using System;
	using System.Threading.Tasks;
	using DotNetOpenAuth.Configuration;
	using DotNetOpenAuth.Messaging;
	using DotNetOpenAuth.Messaging.Bindings;
	using DotNetOpenAuth.Test.Mocks;
	using NUnit.Framework;

	[TestFixture]
	public class StandardExpirationBindingElementTests : MessagingTestBase {
		[Test]
		public async Task SendSetsTimestamp() {
			TestExpiringMessage message = new TestExpiringMessage(MessageTransport.Indirect);
			message.Recipient = new Uri("http://localtest");
			((IExpiringProtocolMessage)message).UtcCreationDate = DateTime.Parse("1/1/1990");

			Channel channel = CreateChannel(MessageProtections.Expiration);
			await channel.PrepareResponseAsync(message);
			Assert.IsTrue(DateTime.UtcNow - ((IExpiringProtocolMessage)message).UtcCreationDate < TimeSpan.FromSeconds(3), "The timestamp on the message was not set on send.");
		}

		[Test]
		public async Task VerifyGoodTimestampIsAccepted() {
			this.Channel = CreateChannel(MessageProtections.Expiration);
			await this.ParameterizedReceiveProtectedTestAsync(DateTime.UtcNow, false);
		}

		[Test]
		public async Task VerifyFutureTimestampWithinClockSkewIsAccepted() {
			this.Channel = CreateChannel(MessageProtections.Expiration);
			await this.ParameterizedReceiveProtectedTestAsync(DateTime.UtcNow + DotNetOpenAuthSection.Messaging.MaximumClockSkew - TimeSpan.FromSeconds(1), false);
		}

		[Test, ExpectedException(typeof(ExpiredMessageException))]
		public async Task VerifyOldTimestampIsRejected() {
			this.Channel = CreateChannel(MessageProtections.Expiration);
			await this.ParameterizedReceiveProtectedTestAsync(DateTime.UtcNow - StandardExpirationBindingElement.MaximumMessageAge - TimeSpan.FromSeconds(1), false);
		}

		[Test, ExpectedException(typeof(ProtocolException))]
		public async Task VerifyFutureTimestampIsRejected() {
			this.Channel = CreateChannel(MessageProtections.Expiration);
			await this.ParameterizedReceiveProtectedTestAsync(DateTime.UtcNow + DotNetOpenAuthSection.Messaging.MaximumClockSkew + TimeSpan.FromSeconds(2), false);
		}
	}
}