summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.Test/OpenId/Provider/PerformanceTests.cs
blob: e2c719dbfbd8d9db71f51e102587367b3d09a2d2 (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
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
//-----------------------------------------------------------------------
// <copyright file="PerformanceTests.cs" company="Outercurve Foundation">
//     Copyright (c) Outercurve Foundation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.Test.OpenId.Provider {
	using System;
	using System.Collections.Generic;
	using System.Diagnostics;
	using System.IO;
	using System.Linq;
	using System.Net;
	using System.Text;
	using DotNetOpenAuth.Messaging;
	using DotNetOpenAuth.Messaging.Reflection;
	using DotNetOpenAuth.OpenId;
	using DotNetOpenAuth.OpenId.ChannelElements;
	using DotNetOpenAuth.OpenId.Messages;
	using DotNetOpenAuth.OpenId.Provider;
	using DotNetOpenAuth.Test.Performance;
	using NUnit.Framework;

	[TestFixture, Category("Performance")]
	public class PerformanceTests : OpenIdTestBase {
		private const string SharedAssociationHandle = "handle";
		private static readonly TimeSpan TestRunTime = TimeSpan.FromSeconds(3);
		private OpenIdProvider provider;

		[SetUp]
		public override void SetUp() {
			base.SetUp();
			this.provider = CreateProvider();
		}

		[Test]
		public void AssociateDH() {
			var associateRequest = this.CreateAssociateRequest(OPUri);
			MeasurePerformance(
				() => {
					IRequest request = this.provider.GetRequest(associateRequest);
					var response = this.provider.PrepareResponse(request);
					Assert.IsInstanceOf<AssociateSuccessfulResponse>(response.OriginalMessage);
				},
				maximumAllowedUnitTime: 3.5e6f,
				iterations: 1);
		}

		[Test]
		public void AssociateClearText() {
			var associateRequest = this.CreateAssociateRequest(OPUriSsl); // SSL will cause a plaintext association
			MeasurePerformance(
				() => {
					IRequest request = this.provider.GetRequest(associateRequest);
					var response = this.provider.PrepareResponse(request);
					Assert.IsInstanceOf<AssociateSuccessfulResponse>(response.OriginalMessage);
				},
				maximumAllowedUnitTime: 1.5e4f,
				iterations: 1000);
		}

		[Test]
		public void CheckIdSharedHmacSha1Association() {
			Protocol protocol = Protocol.Default;
			string assocType = protocol.Args.SignatureAlgorithm.HMAC_SHA1;
			this.ParameterizedCheckIdTest(protocol, assocType);
		}

		[Test]
		public void CheckIdSharedHmacSha256Association() {
			Protocol protocol = Protocol.Default;
			string assocType = protocol.Args.SignatureAlgorithm.HMAC_SHA256;
			this.ParameterizedCheckIdTest(protocol, assocType);
		}

		private void ParameterizedCheckIdTest(Protocol protocol, string assocType) {
			Association assoc = HmacShaAssociationProvider.Create(
				protocol,
				assocType,
				AssociationRelyingPartyType.Smart,
				this.provider.AssociationStore,
				this.provider.SecuritySettings);
			var checkidRequest = this.CreateCheckIdRequest(true);
			MeasurePerformance(
				() => {
					var request = (IAuthenticationRequest)this.provider.GetRequest(checkidRequest);
					request.IsAuthenticated = true;
					var response = this.provider.PrepareResponse(request);
					Assert.IsInstanceOf<PositiveAssertionResponse>(response.OriginalMessage);
				},
				maximumAllowedUnitTime: 6.8e4f);
		}

		private HttpRequestInfo CreateAssociateRequest(Uri opEndpoint) {
			var rp = CreateRelyingParty(true);
			AssociateRequest associateMessage = AssociateRequestRelyingParty.Create(rp.SecuritySettings, new ProviderEndpointDescription(opEndpoint, Protocol.Default.Version));
			Channel rpChannel = rp.Channel;
			MemoryStream ms = new MemoryStream();
			StreamWriter mswriter = new StreamWriter(ms);
			mswriter.Write(MessagingUtilities.CreateQueryString(rpChannel.MessageDescriptions.GetAccessor(associateMessage)));
			mswriter.Flush();
			ms.Position = 0;
			var headers = new WebHeaderCollection();
			headers.Add(HttpRequestHeader.ContentType, Channel.HttpFormUrlEncoded);
			var httpRequest = new HttpRequestInfo("POST", opEndpoint, headers, ms);
			return httpRequest;
		}

		private HttpRequestInfo CreateCheckIdRequest(bool sharedAssociation) {
			var rp = CreateRelyingParty(true);
			CheckIdRequest checkidMessage = new CheckIdRequest(
				Protocol.Default.Version,
				OPUri,
				DotNetOpenAuth.OpenId.AuthenticationRequestMode.Setup);
			if (sharedAssociation) {
				checkidMessage.AssociationHandle = SharedAssociationHandle;
			}
			checkidMessage.ClaimedIdentifier = OPLocalIdentifiers[0];
			checkidMessage.LocalIdentifier = OPLocalIdentifiers[0];
			checkidMessage.Realm = RPRealmUri;
			checkidMessage.ReturnTo = RPUri;
			Channel rpChannel = rp.Channel;
			UriBuilder receiver = new UriBuilder(OPUri);
			receiver.Query = MessagingUtilities.CreateQueryString(rpChannel.MessageDescriptions.GetAccessor(checkidMessage));
			var httpRequest = new HttpRequestInfo("GET", receiver.Uri);
			return httpRequest;
		}
	}
}